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:
HtmlLayout
,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 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(Charset charset)
protected
AbstractStringLayout(Charset aCharset, byte[] header, byte[] footer)
Builds a new layout.protected
AbstractStringLayout(Configuration config, 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(String s)
Charset
getCharset()
Gets the Charset this layout uses to encode Strings into bytes.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 StringBuilder
getStringBuilder()
Returns aStringBuilder
that this Layout implementation can use to write the formatted log event to.protected Encoder<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 String
serializeToString(AbstractStringLayout.Serializer serializer)
byte[]
toByteArray(LogEvent event)
Formats the Log Event as a byte array.protected static void
trimToMaxSize(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(Charset charset)
-
AbstractStringLayout
protected AbstractStringLayout(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, 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
-
requiresLocation
public boolean requiresLocation()
- Specified by:
requiresLocation
in interfaceLocationAware
-
getStringBuilder
protected static 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(StringBuilder stringBuilder)
-
getBytes
protected byte[] getBytes(String s)
-
getCharset
public 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 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<String>
- Returns:
- The default content type for Strings.
-
getFooter
public byte[] getFooter()
Returns the footer, if one is available.
-
getFooterSerializer
public AbstractStringLayout.Serializer getFooterSerializer()
-
getHeader
public byte[] getHeader()
Returns the header, if one is available.
-
getHeaderSerializer
public AbstractStringLayout.Serializer getHeaderSerializer()
-
getStringBuilderEncoder
protected Encoder<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 String serializeToString(AbstractStringLayout.Serializer serializer)
-
toByteArray
public byte[] toByteArray(LogEvent event)
Formats the Log Event as a byte array.- Specified by:
toByteArray
in interfaceLayout<String>
- Parameters:
event
- The Log Event.- Returns:
- The formatted event as a byte array.
-
-