Class AbstractStringLayout
- java.lang.Object
-
- org.apache.logging.log4j.core.layout.AbstractLayout<java.lang.String>
-
- org.apache.logging.log4j.core.layout.AbstractStringLayout
-
- All Implemented Interfaces:
Layout<java.lang.String>
,Encoder<LogEvent>
,StringLayout
- Direct Known Subclasses:
AbstractCsvLayout
,GelfLayout
,HtmlLayout
,JsonLayout
,Log4j1XmlLayout
,PatternLayout
,Rfc5424Layout
,SyslogLayout
,XmlLayout
,YamlLayout
public abstract class AbstractStringLayout extends AbstractLayout<java.lang.String> implements StringLayout
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 Classes Modifier and Type Class Description static class
AbstractStringLayout.Builder<B extends AbstractStringLayout.Builder<B>>
static interface
AbstractStringLayout.Serializer
static interface
AbstractStringLayout.Serializer2
Variation ofAbstractStringLayout.Serializer
that avoids allocating temporary objects.
-
Field Summary
Fields Modifier and Type Field Description protected static int
DEFAULT_STRING_BUILDER_SIZE
Default length for new StringBuilder instances: 1024 .protected static int
MAX_STRING_BUILDER_SIZE
-
Fields inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
configuration, eventCount, footer, header, LOGGER
-
Fields inherited from interface org.apache.logging.log4j.core.Layout
ELEMENT_TYPE
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractStringLayout(java.nio.charset.Charset charset)
protected
AbstractStringLayout(java.nio.charset.Charset aCharset, byte[] header, byte[] footer)
Builds a new layout.protected
AbstractStringLayout(Configuration config, java.nio.charset.Charset aCharset, AbstractStringLayout.Serializer headerSerializer, AbstractStringLayout.Serializer footerSerializer)
Builds a new layout.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected byte[]
getBytes(java.lang.String s)
java.nio.charset.Charset
getCharset()
Gets the Charset this layout uses to encode Strings into bytes.java.lang.String
getContentType()
Returns the content type output by this layout.byte[]
getFooter()
Returns the footer, if one is available.AbstractStringLayout.Serializer
getFooterSerializer()
byte[]
getHeader()
Returns the header, if one is available.AbstractStringLayout.Serializer
getHeaderSerializer()
protected static java.lang.StringBuilder
getStringBuilder()
Returns aStringBuilder
that this Layout implementation can use to write the formatted log event to.protected Encoder<java.lang.StringBuilder>
getStringBuilderEncoder()
Returns aEncoder<StringBuilder>
that this Layout implementation can use for encoding log events.protected byte[]
serializeToBytes(AbstractStringLayout.Serializer serializer, byte[] defaultValue)
protected java.lang.String
serializeToString(AbstractStringLayout.Serializer serializer)
byte[]
toByteArray(LogEvent event)
Formats the Log Event as a byte array.protected static void
trimToMaxSize(java.lang.StringBuilder stringBuilder)
-
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
encode, getConfiguration, getContentFormat, markEvent
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.logging.log4j.core.Layout
getContentFormat, toSerializable
-
-
-
-
Field Detail
-
DEFAULT_STRING_BUILDER_SIZE
protected static final int DEFAULT_STRING_BUILDER_SIZE
Default length for new StringBuilder instances: 1024 .- See Also:
- Constant Field Values
-
MAX_STRING_BUILDER_SIZE
protected static final int MAX_STRING_BUILDER_SIZE
-
-
Constructor Detail
-
AbstractStringLayout
protected AbstractStringLayout(java.nio.charset.Charset charset)
-
AbstractStringLayout
protected AbstractStringLayout(java.nio.charset.Charset aCharset, byte[] header, byte[] footer)
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, java.nio.charset.Charset aCharset, AbstractStringLayout.Serializer headerSerializer, AbstractStringLayout.Serializer footerSerializer)
Builds a new layout.- Parameters:
config
- the configurationaCharset
- 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 Detail
-
getStringBuilder
protected static java.lang.StringBuilder getStringBuilder()
Returns aStringBuilder
that this Layout implementation can use to write the formatted log event to.- Returns:
- a
StringBuilder
-
trimToMaxSize
protected static void trimToMaxSize(java.lang.StringBuilder stringBuilder)
-
getBytes
protected byte[] getBytes(java.lang.String s)
-
getCharset
public java.nio.charset.Charset getCharset()
Description copied from interface:StringLayout
Gets the Charset this layout uses to encode Strings into bytes.- Specified by:
getCharset
in interfaceStringLayout
- Returns:
- the Charset this layout uses to encode Strings into bytes.
-
getContentType
public java.lang.String getContentType()
Description copied from interface:Layout
Returns the content type output by this layout. The base class returns "text/plain".- Specified by:
getContentType
in interfaceLayout<java.lang.String>
- Returns:
- The default content type for Strings.
-
getFooter
public byte[] getFooter()
Returns the footer, if one is available.- Specified by:
getFooter
in interfaceLayout<java.lang.String>
- Overrides:
getFooter
in classAbstractLayout<java.lang.String>
- Returns:
- A byte array containing the footer.
-
getFooterSerializer
public AbstractStringLayout.Serializer getFooterSerializer()
-
getHeader
public byte[] getHeader()
Returns the header, if one is available.- Specified by:
getHeader
in interfaceLayout<java.lang.String>
- Overrides:
getHeader
in classAbstractLayout<java.lang.String>
- Returns:
- A byte array containing the header.
-
getHeaderSerializer
public AbstractStringLayout.Serializer getHeaderSerializer()
-
getStringBuilderEncoder
protected Encoder<java.lang.StringBuilder> getStringBuilderEncoder()
Returns aEncoder<StringBuilder>
that this Layout implementation can use for encoding log events.- Returns:
- a
Encoder<StringBuilder>
-
serializeToBytes
protected byte[] serializeToBytes(AbstractStringLayout.Serializer serializer, byte[] defaultValue)
-
serializeToString
protected java.lang.String serializeToString(AbstractStringLayout.Serializer serializer)
-
toByteArray
public byte[] toByteArray(LogEvent event)
Formats the Log Event as a byte array.- Specified by:
toByteArray
in interfaceLayout<java.lang.String>
- Parameters:
event
- The Log Event.- Returns:
- The formatted event as a byte array.
-
-