Class AbstractStringLayout
java.lang.Object
org.apache.logging.log4j.core.layout.AbstractLayout<String>
org.apache.logging.log4j.core.layout.AbstractStringLayout
- All Implemented Interfaces:
LocationAware, Layout<String>, Encoder<LogEvent>, StringLayout
- Direct Known Subclasses:
AbstractCsvLayout, AbstractJacksonLayout, GelfLayout, HtmlLayout, Log4j1SyslogLayout, Log4j1XmlLayout, PatternLayout, Rfc5424Layout, SyslogLayout
public abstract class AbstractStringLayout
extends AbstractLayout<String>
implements StringLayout, LocationAware
Abstract base class for Layouts that result in a String.
Since 2.4.1, this class has custom logic to convert ISO-8859-1 or US-ASCII Strings to byte[] arrays to improve performance: all characters are simply cast to bytes.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interfacestatic interfaceVariation ofAbstractStringLayout.Serializerthat avoids allocating temporary objects. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CharsetThe charset for the formatted message.protected static final intDefault length for new StringBuilder instances: 1024 .private final AbstractStringLayout.Serializerprivate final AbstractStringLayout.Serializerprotected static final intprivate Encoder<StringBuilder> private static final ThreadLocal<StringBuilder> Fields inherited from class AbstractLayout
configuration, eventCount, footer, header, LOGGERFields inherited from interface Layout
ELEMENT_TYPE -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractStringLayout(Charset charset) protectedAbstractStringLayout(Charset aCharset, byte[] header, byte[] footer) Builds a new layout.protectedAbstractStringLayout(Configuration config, Charset aCharset, AbstractStringLayout.Serializer headerSerializer, AbstractStringLayout.Serializer footerSerializer) Builds a new layout. -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]Gets the Charset this layout uses to encode Strings into bytes.Returns the content type output by this layout.byte[]Returns the footer, if one is available.byte[]Returns the header, if one is available.private DefaultLogEventFactoryprotected static StringBuilderReturns aStringBuilderthat this Layout implementation can use to write the formatted log event to.protected Encoder<StringBuilder> Returns aEncoder<StringBuilder>that this Layout implementation can use for encoding log events.booleanprotected byte[]serializeToBytes(AbstractStringLayout.Serializer serializer, byte[] defaultValue) protected StringserializeToString(AbstractStringLayout.Serializer serializer) private static intbyte[]toByteArray(LogEvent event) Formats the Log Event as a byte array.protected static voidtrimToMaxSize(StringBuilder stringBuilder) Methods inherited from class AbstractLayout
encode, getConfiguration, getContentFormat, markEventMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Layout
getContentFormat, toSerializable
-
Field Details
-
DEFAULT_STRING_BUILDER_SIZE
protected static final int DEFAULT_STRING_BUILDER_SIZEDefault length for new StringBuilder instances: 1024 .- See Also:
-
MAX_STRING_BUILDER_SIZE
protected static final int MAX_STRING_BUILDER_SIZE -
threadLocal
-
textEncoder
-
charset
The charset for the formatted message. -
headerSerializer
-
-
Constructor Details
-
AbstractStringLayout
-
AbstractStringLayout
Builds a new layout.- Parameters:
aCharset- the charset used to encode the header bytes, footer bytes and anything else that needs to be converted from strings to bytes.header- the header bytesfooter- the footer bytes
-
AbstractStringLayout
protected AbstractStringLayout(Configuration config, Charset aCharset, AbstractStringLayout.Serializer headerSerializer, AbstractStringLayout.Serializer footerSerializer) Builds a new layout.- Parameters:
config- the configuration. May be null.aCharset- the charset used to encode the header bytes, footer bytes and anything else that needs to be converted from strings to bytes.headerSerializer- the header bytes serializerfooterSerializer- the footer bytes serializer
-
-
Method Details
-
requiresLocation
public boolean requiresLocation()- Specified by:
requiresLocationin interfaceLocationAware
-
getStringBuilder
Returns aStringBuilderthat this Layout implementation can use to write the formatted log event to.- Returns:
- a
StringBuilder
-
size
-
trimToMaxSize
-
getBytes
-
getCharset
Description copied from interface:StringLayoutGets the Charset this layout uses to encode Strings into bytes.- Specified by:
getCharsetin interfaceStringLayout- Returns:
- the Charset this layout uses to encode Strings into bytes.
-
getContentType
Description copied from interface:LayoutReturns the content type output by this layout. The base class returns "text/plain".- Specified by:
getContentTypein interfaceLayout<String>- Returns:
- The default content type for Strings.
-
getHeader
-
getHeaderSerializer
-
getLogEventFactory
-
getStringBuilderEncoder
Returns aEncoder<StringBuilder>that this Layout implementation can use for encoding log events.- Returns:
- a
Encoder<StringBuilder>
-
serializeToBytes
-
serializeToString
-
toByteArray
Formats the Log Event as a byte array.- Specified by:
toByteArrayin interfaceLayout<String>- Parameters:
event- The Log Event.- Returns:
- The formatted event as a byte array.
-