Class PatternLayout
java.lang.Object
org.apache.logging.log4j.core.layout.AbstractLayout<String>
org.apache.logging.log4j.core.layout.AbstractStringLayout
org.apache.logging.log4j.core.layout.PatternLayout
- All Implemented Interfaces:
LocationAware
,Layout<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
a LogEvent
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
Modifier and TypeClassDescriptionstatic class
Custom PatternLayout builder.static class
Nested classes/interfaces inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Default pattern string for log output.static final String
Key to identify pattern converters.static final String
A simple pattern.static final String
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic PatternLayout
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
(String pattern, PatternSelector patternSelector, Configuration config, RegexReplacement replace, Charset charset, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi, String headerPattern, String footerPattern) Deprecated.static PatternParser
createPatternParser
(Configuration config) Creates a PatternParser.createSerializer
(Configuration configuration, RegexReplacement replace, String pattern, 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.Gets this PatternLayout's content format.Gets the conversion pattern.static PatternLayout.Builder
Creates a builder for a custom PatternLayout.boolean
void
serialize
(LogEvent event, StringBuilder stringBuilder) toSerializable
(LogEvent event) Formats a logging event to a writer.toString()
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 Details
-
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:
-
TTCC_CONVERSION_PATTERN
A conversion pattern equivalent to the TTCCLayout. Current value is %r [%t] %p %c %notEmpty{%x }- %m%n.- See Also:
-
SIMPLE_CONVERSION_PATTERN
A simple pattern. Current value is %d [%t] %p %c - %m%n.- See Also:
-
KEY
Key to identify pattern converters.- See Also:
-
-
Method Details
-
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, String pattern, String defaultPattern, PatternSelector patternSelector, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi) Deprecated.UsenewSerializerBuilder()
instead.Deprecated, usenewSerializerBuilder()
instead.- Parameters:
configuration
- the current configurationreplace
- Allows portions of the resulting String to be replaced.pattern
- the current patterndefaultPattern
- the default patternpatternSelector
- Allows different patterns to be used with the PatternLayout based on some selection criteria.alwaysWriteExceptions
- To always write exceptions even if the pattern contains no exception conversions.noConsoleNoAnsi
- Do not output ANSI escape codes if System.console() is null.- Returns:
- a new Serializer
-
getConversionPattern
Gets the conversion pattern.- Returns:
- the conversion pattern.
-
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<String>
- Overrides:
getContentFormat
in classAbstractLayout<String>
- Returns:
- Map of content format keys supporting PatternLayout
-
toSerializable
Formats a logging event to a writer.- Parameters:
event
- logging event to be formatted.- Returns:
- The event formatted as a String.
-
serialize
-
encode
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 { @Override public void encode(LogEvent event, ByteBufferDestination destination) { StringBuilder text = getStringBuilder(); convertLogEventToText(event, text); getStringBuilderEncoder().encode(text, destination); } private void convertLogEventToText(LogEvent event, StringBuilder destination) { ... // append a text representation of the log event to the StringBuilder } }
-
createPatternParser
Creates a PatternParser.- Parameters:
config
- The Configuration ornull
.- Returns:
- The PatternParser.
-
toString
-
createLayout
@PluginFactory @Deprecated public static PatternLayout createLayout(@PluginAttribute(value="pattern",defaultString="%m%n") String pattern, @PluginElement("PatternSelector") PatternSelector patternSelector, @PluginConfiguration Configuration config, @PluginElement("Replace") RegexReplacement replace, @PluginAttribute("charset") Charset charset, @PluginAttribute(value="alwaysWriteExceptions",defaultBoolean=true) boolean alwaysWriteExceptions, @PluginAttribute("noConsoleNoAnsi") boolean noConsoleNoAnsi, @PluginAttribute("header") String headerPattern, @PluginAttribute("footer") 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
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:
-
createDefaultLayout
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:
-
newBuilder
Creates a builder for a custom PatternLayout.- Returns:
- a PatternLayout builder.
-
getEventSerializer
-
newBuilder()
instead.