@Plugin(name="PatternLayout", category="Core", elementType="layout", printObject=true) public final class PatternLayout extends AbstractStringLayout
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.
Modifier and Type | Class and Description |
---|---|
static class |
PatternLayout.Builder
Custom PatternLayout builder.
|
static class |
PatternLayout.SerializerBuilder |
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_CONVERSION_PATTERN
Default pattern string for log output.
|
static String |
KEY
Key to identify pattern converters.
|
static String |
SIMPLE_CONVERSION_PATTERN
A simple pattern.
|
static String |
TTCC_CONVERSION_PATTERN
A conversion pattern equivalent to the TTCCLayout.
|
DEFAULT_STRING_BUILDER_SIZE, MAX_STRING_BUILDER_SIZE
configuration, eventCount, footer, header, LOGGER
ELEMENT_TYPE
Modifier and Type | Method and 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(String pattern,
PatternSelector patternSelector,
Configuration config,
RegexReplacement replace,
Charset charset,
boolean alwaysWriteExceptions,
boolean noConsoleNoAnsi,
String headerPattern,
String footerPattern)
Deprecated.
Use
newBuilder() instead. This will be private in a future version. |
static PatternParser |
createPatternParser(Configuration config)
Creates a PatternParser.
|
static AbstractStringLayout.Serializer |
createSerializer(Configuration configuration,
RegexReplacement replace,
String pattern,
String defaultPattern,
PatternSelector patternSelector,
boolean alwaysWriteExceptions,
boolean noConsoleNoAnsi)
Deprecated.
Use
newSerializerBuilder() instead. |
void |
encode(LogEvent event,
ByteBufferDestination destination)
Encodes the specified source LogEvent to some binary representation and writes the result to the specified
destination.
|
Map<String,String> |
getContentFormat()
Gets this PatternLayout's content format.
|
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() |
String |
toSerializable(LogEvent event)
Formats a logging event to a writer.
|
String |
toString() |
getBytes, getCharset, getContentType, getFooter, getFooterSerializer, getHeader, getHeaderSerializer, getStringBuilder, getStringBuilderEncoder, serializeToBytes, serializeToString, toByteArray, trimToMaxSize
getConfiguration, markEvent
public static final String DEFAULT_CONVERSION_PATTERN
public static final String TTCC_CONVERSION_PATTERN
public static final String SIMPLE_CONVERSION_PATTERN
public static final String KEY
public static PatternLayout.SerializerBuilder newSerializerBuilder()
public boolean requiresLocation()
requiresLocation
in interface LocationAware
requiresLocation
in class AbstractStringLayout
@Deprecated public static AbstractStringLayout.Serializer createSerializer(Configuration configuration, RegexReplacement replace, String pattern, String defaultPattern, PatternSelector patternSelector, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi)
newSerializerBuilder()
instead.newSerializerBuilder()
instead.configuration
- replace
- pattern
- defaultPattern
- patternSelector
- alwaysWriteExceptions
- noConsoleNoAnsi
- public String getConversionPattern()
public Map<String,String> getContentFormat()
getContentFormat
in interface Layout<String>
getContentFormat
in class AbstractLayout<String>
public String toSerializable(LogEvent event)
event
- logging event to be formatted.public void encode(LogEvent event, ByteBufferDestination destination)
AbstractLayout
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 {
encode
in interface Encoder<LogEvent>
encode
in class AbstractLayout<String>
event
- the LogEvent to encode.destination
- holds the ByteBuffer to write into.AbstractStringLayout.getStringBuilder()
,
AbstractStringLayout.getStringBuilderEncoder()
public static PatternParser createPatternParser(Configuration config)
config
- The Configuration.@PluginFactory @Deprecated public static PatternLayout createLayout(@PluginAttribute(value="pattern",defaultString="%m%n") String pattern, @PluginElement(value="PatternSelector") PatternSelector patternSelector, @PluginConfiguration Configuration config, @PluginElement(value="Replace") RegexReplacement replace, @PluginAttribute(value="charset") Charset charset, @PluginAttribute(value="alwaysWriteExceptions",defaultBoolean=true) boolean alwaysWriteExceptions, @PluginAttribute(value="noConsoleNoAnsi") boolean noConsoleNoAnsi, @PluginAttribute(value="header") String headerPattern, @PluginAttribute(value="footer") String footerPattern)
newBuilder()
instead. This will be private in a future version.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) and System.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.public static PatternLayout createDefaultLayout()
Default conversion pattern
public static PatternLayout createDefaultLayout(Configuration configuration)
configuration
- The Configuration.Default conversion pattern
@PluginBuilderFactory public static PatternLayout.Builder newBuilder()
public AbstractStringLayout.Serializer getEventSerializer()
Copyright © 1999-2021 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.