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
,GelfLayout
,HtmlLayout
,JsonLayout
,PatternLayout
,Rfc5424Layout
,SyslogLayout
,XmlLayout
,YamlLayout
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
Modifier and TypeClassDescriptionstatic class
static interface
static interface
Variation ofAbstractStringLayout.Serializer
that avoids allocating temporary objects. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final int
Default length for new StringBuilder instances: 1024 .protected static final int
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
ModifierConstructorDescriptionprotected
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
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.protected static StringBuilder
Returns aStringBuilder
that 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.boolean
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 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
-
-
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:
requiresLocation
in interfaceLocationAware
-
getStringBuilder
Returns aStringBuilder
that this Layout implementation can use to write the formatted log event to.- Returns:
- a
StringBuilder
-
trimToMaxSize
-
getBytes
-
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
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.
-
getHeader
public byte[] getHeader()Returns the header, if one is available. -
getHeaderSerializer
-
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:
toByteArray
in interfaceLayout<String>
- Parameters:
event
- The Log Event.- Returns:
- The formatted event as a byte array.
-