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:
LocationAware
,Layout<java.lang.String>
,Encoder<LogEvent>
,StringLayout
- Direct Known Subclasses:
AbstractCsvLayout
,AbstractJacksonLayout
,GelfLayout
,HtmlLayout
,Log4j1XmlLayout
,PatternLayout
,Rfc5424Layout
,SyslogLayout
public abstract class AbstractStringLayout extends AbstractLayout<java.lang.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 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 private java.nio.charset.Charset
charset
The charset for the formatted message.protected static int
DEFAULT_STRING_BUILDER_SIZE
Default length for new StringBuilder instances: 1024 .private AbstractStringLayout.Serializer
footerSerializer
private AbstractStringLayout.Serializer
headerSerializer
protected static int
MAX_STRING_BUILDER_SIZE
private Encoder<java.lang.StringBuilder>
textEncoder
private static java.lang.ThreadLocal<java.lang.StringBuilder>
threadLocal
-
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()
private DefaultLogEventFactory
getLogEventFactory()
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.boolean
requiresLocation()
protected byte[]
serializeToBytes(AbstractStringLayout.Serializer serializer, byte[] defaultValue)
protected java.lang.String
serializeToString(AbstractStringLayout.Serializer serializer)
private static int
size(java.lang.String property, int defaultValue)
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
-
threadLocal
private static final java.lang.ThreadLocal<java.lang.StringBuilder> threadLocal
-
textEncoder
private Encoder<java.lang.StringBuilder> textEncoder
-
charset
private final java.nio.charset.Charset charset
The charset for the formatted message.
-
footerSerializer
private final AbstractStringLayout.Serializer footerSerializer
-
headerSerializer
private final AbstractStringLayout.Serializer headerSerializer
-
-
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 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 Detail
-
requiresLocation
public boolean requiresLocation()
- Specified by:
requiresLocation
in interfaceLocationAware
-
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
-
size
private static int size(java.lang.String property, int defaultValue)
-
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()
-
getLogEventFactory
private DefaultLogEventFactory getLogEventFactory()
-
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.
-
-