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,Log4j1SyslogLayout,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 classAbstractStringLayout.Builder<B extends AbstractStringLayout.Builder<B>>static interfaceAbstractStringLayout.Serializerstatic interfaceAbstractStringLayout.Serializer2Variation ofAbstractStringLayout.Serializerthat avoids allocating temporary objects.
-
Field Summary
Fields Modifier and Type Field Description private java.nio.charset.CharsetcharsetThe charset for the formatted message.protected static intDEFAULT_STRING_BUILDER_SIZEDefault length for new StringBuilder instances: 1024 .private AbstractStringLayout.SerializerfooterSerializerprivate AbstractStringLayout.SerializerheaderSerializerprotected static intMAX_STRING_BUILDER_SIZEprivate Encoder<java.lang.StringBuilder>textEncoderprivate 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 protectedAbstractStringLayout(java.nio.charset.Charset charset)protectedAbstractStringLayout(java.nio.charset.Charset aCharset, byte[] header, byte[] footer)Builds a new layout.protectedAbstractStringLayout(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.CharsetgetCharset()Gets the Charset this layout uses to encode Strings into bytes.java.lang.StringgetContentType()Returns the content type output by this layout.byte[]getFooter()Returns the footer, if one is available.AbstractStringLayout.SerializergetFooterSerializer()byte[]getHeader()Returns the header, if one is available.AbstractStringLayout.SerializergetHeaderSerializer()private DefaultLogEventFactorygetLogEventFactory()protected static java.lang.StringBuildergetStringBuilder()Returns aStringBuilderthat 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.booleanrequiresLocation()protected byte[]serializeToBytes(AbstractStringLayout.Serializer serializer, byte[] defaultValue)protected java.lang.StringserializeToString(AbstractStringLayout.Serializer serializer)private static intsize(java.lang.String property, int defaultValue)byte[]toByteArray(LogEvent event)Formats the Log Event as a byte array.protected static voidtrimToMaxSize(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:
requiresLocationin interfaceLocationAware
-
getStringBuilder
protected static java.lang.StringBuilder getStringBuilder()
Returns aStringBuilderthat 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: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
public java.lang.String getContentType()
Description copied from interface:LayoutReturns the content type output by this layout. The base class returns "text/plain".- Specified by:
getContentTypein interfaceLayout<java.lang.String>- Returns:
- The default content type for Strings.
-
getFooter
public byte[] getFooter()
Returns the footer, if one is available.- Specified by:
getFooterin interfaceLayout<java.lang.String>- Overrides:
getFooterin 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:
getHeaderin interfaceLayout<java.lang.String>- Overrides:
getHeaderin 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:
toByteArrayin interfaceLayout<java.lang.String>- Parameters:
event- The Log Event.- Returns:
- The formatted event as a byte array.
-
-