@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.
|
private static class |
PatternLayout.PatternSelectorSerializer |
private static class |
PatternLayout.PatternSerializer |
static class |
PatternLayout.SerializerBuilder |
AbstractStringLayout.Serializer, AbstractStringLayout.Serializer2
Modifier and Type | Field and 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.
|
DEFAULT_STRING_BUILDER_SIZE, MAX_STRING_BUILDER_SIZE
configuration, eventCount, footer, header, LOGGER
ELEMENT_TYPE
Modifier | Constructor and 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.
|
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(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.
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,
java.lang.String pattern,
java.lang.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.
|
java.util.Map<java.lang.String,java.lang.String> |
getContentFormat()
Gets this PatternLayout's content format.
|
java.lang.String |
getConversionPattern()
Gets the conversion pattern.
|
static PatternLayout.Builder |
newBuilder()
Creates a builder for a custom PatternLayout.
|
static PatternLayout.SerializerBuilder |
newSerializerBuilder() |
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.
|
getBytes, getCharset, getContentType, getFooter, getFooterSerializer, getHeader, getHeaderSerializer, getStringBuilder, getStringBuilderEncoder, serializeToBytes, serializeToString, toByteArray, trimToMaxSize
getConfiguration, markEvent, writeTo
public static final java.lang.String DEFAULT_CONVERSION_PATTERN
public static final java.lang.String TTCC_CONVERSION_PATTERN
public static final java.lang.String SIMPLE_CONVERSION_PATTERN
public static final java.lang.String KEY
private final java.lang.String conversionPattern
private final PatternSelector patternSelector
private final AbstractStringLayout.Serializer eventSerializer
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)
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 (if true
,
exceptions will be written even if the pattern does not specify so).disableAnsi
- If "true"
, do not output ANSI escape codesnoConsoleNoAnsi
- If "true"
(default) and System.console()
is null, do not output ANSI escape codesheaderPattern
- header conversion pattern.footerPattern
- footer conversion pattern.public static PatternLayout.SerializerBuilder newSerializerBuilder()
@Deprecated public static AbstractStringLayout.Serializer createSerializer(Configuration configuration, RegexReplacement replace, java.lang.String pattern, java.lang.String defaultPattern, PatternSelector patternSelector, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi)
newSerializerBuilder()
instead.newSerializerBuilder()
instead.configuration
- replace
- pattern
- defaultPattern
- patternSelector
- alwaysWriteExceptions
- noConsoleNoAnsi
- public java.lang.String getConversionPattern()
public java.util.Map<java.lang.String,java.lang.String> getContentFormat()
getContentFormat
in interface Layout<java.lang.String>
getContentFormat
in class AbstractLayout<java.lang.String>
public java.lang.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<java.lang.String>
event
- the LogEvent to encode.destination
- holds the ByteBuffer to write into.AbstractStringLayout.getStringBuilder()
,
AbstractStringLayout.getStringBuilderEncoder()
private java.lang.StringBuilder toText(AbstractStringLayout.Serializer2 serializer, LogEvent event, java.lang.StringBuilder destination)
Implementations are free to return a new StringBuilder if they can detect in advance that the specified StringBuilder is too small.
public static PatternParser createPatternParser(Configuration config)
config
- The Configuration.public java.lang.String toString()
toString
in class java.lang.Object
@PluginFactory @Deprecated public static PatternLayout createLayout(@PluginAttribute(value="pattern",defaultString="%m%n") java.lang.String pattern, @PluginElement(value="PatternSelector") PatternSelector patternSelector, @PluginConfiguration Configuration config, @PluginElement(value="Replace") RegexReplacement replace, @PluginAttribute(value="charset") java.nio.charset.Charset charset, @PluginAttribute(value="alwaysWriteExceptions",defaultBoolean=true) boolean alwaysWriteExceptions, @PluginAttribute(value="noConsoleNoAnsi") boolean noConsoleNoAnsi, @PluginAttribute(value="header") java.lang.String headerPattern, @PluginAttribute(value="footer") java.lang.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()