Class PatternLayout
- java.lang.Object
-
- org.apache.logging.log4j.core.layout.AbstractLayout<java.lang.String>
-
- org.apache.logging.log4j.core.layout.AbstractStringLayout
-
- org.apache.logging.log4j.core.layout.PatternLayout
-
- All Implemented Interfaces:
LocationAware
,Layout<java.lang.String>
,Encoder<LogEvent>
,StringLayout
@Plugin(name="PatternLayout", category="Core", elementType="layout", printObject=true) public final class PatternLayout extends AbstractStringLayout
A flexible layout configurable with pattern string.The goal of this class is to
format
aLogEvent
and return the results. The format of the result depends on the conversion pattern.The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions called conversion specifiers.
See the Log4j Manual for details on the supported pattern converters.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PatternLayout.Builder
Custom PatternLayout builder.private static class
PatternLayout.PatternSelectorSerializer
private static class
PatternLayout.PatternSerializer
static class
PatternLayout.SerializerBuilder
-
Nested classes/interfaces inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
conversionPattern
Conversion pattern.static java.lang.String
DEFAULT_CONVERSION_PATTERN
Default pattern string for log output.private AbstractStringLayout.Serializer
eventSerializer
static java.lang.String
KEY
Key to identify pattern converters.private PatternSelector
patternSelector
static java.lang.String
SIMPLE_CONVERSION_PATTERN
A simple pattern.static java.lang.String
TTCC_CONVERSION_PATTERN
A conversion pattern equivalent to the TTCCLayout.-
Fields inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
DEFAULT_STRING_BUILDER_SIZE, 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 private
PatternLayout(Configuration config, RegexReplacement replace, java.lang.String eventPattern, PatternSelector patternSelector, java.nio.charset.Charset charset, boolean alwaysWriteExceptions, boolean disableAnsi, boolean noConsoleNoAnsi, java.lang.String headerPattern, java.lang.String footerPattern)
Constructs a PatternLayout using the supplied conversion pattern.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static PatternLayout
createDefaultLayout()
Creates a PatternLayout using the default options.static PatternLayout
createDefaultLayout(Configuration configuration)
Creates a PatternLayout using the default options and the given configuration.static PatternLayout
createLayout(java.lang.String pattern, PatternSelector patternSelector, Configuration config, RegexReplacement replace, java.nio.charset.Charset charset, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi, java.lang.String headerPattern, java.lang.String footerPattern)
Deprecated.UsenewBuilder()
instead.static PatternParser
createPatternParser(Configuration config)
Creates a PatternParser.static AbstractStringLayout.Serializer
createSerializer(Configuration configuration, RegexReplacement replace, java.lang.String pattern, java.lang.String defaultPattern, PatternSelector patternSelector, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi)
Deprecated.UsenewSerializerBuilder()
instead.void
encode(LogEvent event, ByteBufferDestination destination)
Encodes the specified source LogEvent to some binary representation and writes the result to the specified destination.java.util.Map<java.lang.String,java.lang.String>
getContentFormat()
Gets this PatternLayout's content format.java.lang.String
getConversionPattern()
Gets the conversion pattern.AbstractStringLayout.Serializer
getEventSerializer()
static PatternLayout.Builder
newBuilder()
Creates a builder for a custom PatternLayout.static PatternLayout.SerializerBuilder
newSerializerBuilder()
boolean
requiresLocation()
void
serialize(LogEvent event, java.lang.StringBuilder stringBuilder)
java.lang.String
toSerializable(LogEvent event)
Formats a logging event to a writer.java.lang.String
toString()
private java.lang.StringBuilder
toText(AbstractStringLayout.Serializer2 serializer, LogEvent event, java.lang.StringBuilder destination)
Creates a text representation of the specified log event and writes it into the specified StringBuilder.-
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
getBytes, getCharset, getContentType, getFooter, getFooterSerializer, getHeader, getHeaderSerializer, getStringBuilder, getStringBuilderEncoder, serializeToBytes, serializeToString, toByteArray, trimToMaxSize
-
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
getConfiguration, markEvent
-
-
-
-
Field Detail
-
DEFAULT_CONVERSION_PATTERN
public static final java.lang.String DEFAULT_CONVERSION_PATTERN
Default pattern string for log output. Currently set to the string "%m%n" which just prints the application supplied message.- See Also:
- Constant Field Values
-
TTCC_CONVERSION_PATTERN
public static final java.lang.String TTCC_CONVERSION_PATTERN
A conversion pattern equivalent to the TTCCLayout. Current value is %r [%t] %p %c %notEmpty{%x }- %m%n.- See Also:
- Constant Field Values
-
SIMPLE_CONVERSION_PATTERN
public static final java.lang.String SIMPLE_CONVERSION_PATTERN
A simple pattern. Current value is %d [%t] %p %c - %m%n.- See Also:
- Constant Field Values
-
KEY
public static final java.lang.String KEY
Key to identify pattern converters.- See Also:
- Constant Field Values
-
conversionPattern
private final java.lang.String conversionPattern
Conversion pattern.
-
patternSelector
private final PatternSelector patternSelector
-
eventSerializer
private final AbstractStringLayout.Serializer eventSerializer
-
-
Constructor Detail
-
PatternLayout
private PatternLayout(Configuration config, RegexReplacement replace, java.lang.String eventPattern, PatternSelector patternSelector, java.nio.charset.Charset charset, boolean alwaysWriteExceptions, boolean disableAnsi, boolean noConsoleNoAnsi, java.lang.String headerPattern, java.lang.String footerPattern)
Constructs a PatternLayout using the supplied conversion pattern.- Parameters:
config
- The Configuration.replace
- The regular expression to match.eventPattern
- conversion pattern.patternSelector
- The PatternSelector.charset
- The character set.alwaysWriteExceptions
- Whether or not exceptions should always be handled in this pattern (iftrue
, exceptions will be written even if the pattern does not specify so).disableAnsi
- If"true"
, do not output ANSI escape codesnoConsoleNoAnsi
- If"true"
(default) andSystem.console()
is null, do not output ANSI escape codesheaderPattern
- header conversion pattern.footerPattern
- footer conversion pattern.
-
-
Method Detail
-
newSerializerBuilder
public static PatternLayout.SerializerBuilder newSerializerBuilder()
-
requiresLocation
public boolean requiresLocation()
- Specified by:
requiresLocation
in interfaceLocationAware
- Overrides:
requiresLocation
in classAbstractStringLayout
-
createSerializer
@Deprecated public static AbstractStringLayout.Serializer createSerializer(Configuration configuration, RegexReplacement replace, java.lang.String pattern, java.lang.String defaultPattern, PatternSelector patternSelector, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi)
Deprecated.UsenewSerializerBuilder()
instead.Deprecated, usenewSerializerBuilder()
instead.- Parameters:
configuration
-replace
-pattern
-defaultPattern
-patternSelector
-alwaysWriteExceptions
-noConsoleNoAnsi
-- Returns:
- a new Serializer.
-
getConversionPattern
public java.lang.String getConversionPattern()
Gets the conversion pattern.- Returns:
- the conversion pattern.
-
getContentFormat
public java.util.Map<java.lang.String,java.lang.String> getContentFormat()
Gets this PatternLayout's content format. Specified by:- Key: "structured" Value: "false"
- Key: "formatType" Value: "conversion" (format uses the keywords supported by OptionConverter)
- Key: "format" Value: provided "conversionPattern" param
- Specified by:
getContentFormat
in interfaceLayout<java.lang.String>
- Overrides:
getContentFormat
in classAbstractLayout<java.lang.String>
- Returns:
- Map of content format keys supporting PatternLayout
-
toSerializable
public java.lang.String toSerializable(LogEvent event)
Formats a logging event to a writer.- Parameters:
event
- logging event to be formatted.- Returns:
- The event formatted as a String.
-
serialize
public void serialize(LogEvent event, java.lang.StringBuilder stringBuilder)
-
encode
public void encode(LogEvent event, ByteBufferDestination destination)
Description copied from class:AbstractLayout
Encodes the specified source LogEvent to some binary representation and writes the result to the specified destination.The default implementation of this method delegates to the
Layout.toByteArray(LogEvent)
method which allocates temporary objects.Subclasses can override this method to provide a garbage-free implementation. For text-based layouts,
AbstractStringLayout
provides various convenience methods to help with this:@Plugin(name = "MyLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) public final class MyLayout extends AbstractStringLayout {
- Specified by:
encode
in interfaceEncoder<LogEvent>
- Overrides:
encode
in classAbstractLayout<java.lang.String>
- Parameters:
event
- the LogEvent to encode.destination
- holds the ByteBuffer to write into.- See Also:
AbstractStringLayout.getStringBuilder()
,AbstractStringLayout.getStringBuilderEncoder()
-
toText
private java.lang.StringBuilder toText(AbstractStringLayout.Serializer2 serializer, LogEvent event, java.lang.StringBuilder destination)
Creates a text representation of the specified log event and writes it into the specified StringBuilder.Implementations are free to return a new StringBuilder if they can detect in advance that the specified StringBuilder is too small.
-
createPatternParser
public static PatternParser createPatternParser(Configuration config)
Creates a PatternParser.- Parameters:
config
- The Configuration.- Returns:
- The PatternParser.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
createLayout
@PluginFactory @Deprecated public static PatternLayout createLayout(@PluginAttribute(value="pattern",defaultString="%m%n") java.lang.String pattern, @PluginElement("PatternSelector") PatternSelector patternSelector, @PluginConfiguration Configuration config, @PluginElement("Replace") RegexReplacement replace, @PluginAttribute("charset") java.nio.charset.Charset charset, @PluginAttribute(value="alwaysWriteExceptions",defaultBoolean=true) boolean alwaysWriteExceptions, @PluginAttribute("noConsoleNoAnsi") boolean noConsoleNoAnsi, @PluginAttribute("header") java.lang.String headerPattern, @PluginAttribute("footer") java.lang.String footerPattern)
Deprecated.UsenewBuilder()
instead. This will be private in a future version.Creates a pattern layout.- Parameters:
pattern
- The pattern. If not specified, defaults to DEFAULT_CONVERSION_PATTERN.patternSelector
- Allows different patterns to be used based on some selection criteria.config
- The Configuration. Some Converters require access to the Interpolator.replace
- A Regex replacement String.charset
- The character set. The platform default is used if not specified.alwaysWriteExceptions
- If"true"
(default) exceptions are always written even if the pattern contains no exception tokens.noConsoleNoAnsi
- If"true"
(default is false) andSystem.console()
is null, do not output ANSI escape codesheaderPattern
- The footer to place at the top of the document, once.footerPattern
- The footer to place at the bottom of the document, once.- Returns:
- The PatternLayout.
-
createDefaultLayout
public static PatternLayout createDefaultLayout()
Creates a PatternLayout using the default options. These options include using UTF-8, the default conversion pattern, exceptions being written, and with ANSI escape codes.- Returns:
- the PatternLayout.
- See Also:
Default conversion pattern
-
createDefaultLayout
public static PatternLayout createDefaultLayout(Configuration configuration)
Creates a PatternLayout using the default options and the given configuration. These options include using UTF-8, the default conversion pattern, exceptions being written, and with ANSI escape codes.- Parameters:
configuration
- The Configuration.- Returns:
- the PatternLayout.
- See Also:
Default conversion pattern
-
newBuilder
@PluginBuilderFactory public static PatternLayout.Builder newBuilder()
Creates a builder for a custom PatternLayout.- Returns:
- a PatternLayout builder.
-
getEventSerializer
public AbstractStringLayout.Serializer getEventSerializer()
-
-