org.apache.logging.log4j.core.layout
Class PatternLayout

java.lang.Object
  extended by org.apache.logging.log4j.core.layout.AbstractLayout<String>
      extended by org.apache.logging.log4j.core.layout.AbstractStringLayout
          extended by org.apache.logging.log4j.core.layout.PatternLayout
All Implemented Interfaces:
Serializable, Layout<String>

@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.

See Also:
Serialized Form

Nested Class Summary
static class PatternLayout.Builder
          Custom PatternLayout builder.
 
Field Summary
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 TTCCCLayout.
 
Fields inherited from class org.apache.logging.log4j.core.layout.AbstractLayout
footer, header, LOGGER
 
Fields inherited from interface org.apache.logging.log4j.core.Layout
ELEMENT_TYPE
 
Method Summary
static PatternLayout createDefaultLayout()
          Creates a PatternLayout using the default options.
static PatternLayout createLayout(String pattern, Configuration config, RegexReplacement replace, Charset charset, boolean alwaysWriteExceptions, boolean noConsoleNoAnsi, String header, String footer)
          Create a pattern layout.
static PatternParser createPatternParser(Configuration config)
          Create a PatternParser.
 Map<String,String> getContentFormat()
          Gets this PatternLayout's content format.
 String getConversionPattern()
          Gets the conversion pattern.
 byte[] getFooter()
          Returns the footer, if one is available.
 byte[] getHeader()
          Returns the header, if one is available.
static PatternLayout.Builder newBuilder()
          Creates a builder for a custom PatternLayout.
 String toSerializable(LogEvent event)
          Formats a logging event to a writer.
 String toString()
           
 
Methods inherited from class org.apache.logging.log4j.core.layout.AbstractStringLayout
getCharset, getContentType, toByteArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CONVERSION_PATTERN

public static final 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 String TTCC_CONVERSION_PATTERN
A conversion pattern equivalent to the TTCCCLayout. Current value is %r [%t] %p %c %x - %m%n.

See Also:
Constant Field Values

SIMPLE_CONVERSION_PATTERN

public static final 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 String KEY
Key to identify pattern converters.

See Also:
Constant Field Values
Method Detail

getHeader

public byte[] getHeader()
Description copied from class: AbstractLayout
Returns the header, if one is available.

Specified by:
getHeader in interface Layout<String>
Overrides:
getHeader in class AbstractLayout<String>
Returns:
A byte array containing the header.

getFooter

public byte[] getFooter()
Description copied from class: AbstractLayout
Returns the footer, if one is available.

Specified by:
getFooter in interface Layout<String>
Overrides:
getFooter in class AbstractLayout<String>
Returns:
A byte array containing the footer.

getConversionPattern

public String getConversionPattern()
Gets the conversion pattern.

Returns:
the conversion pattern.

getContentFormat

public Map<String,String> getContentFormat()
Gets this PatternLayout's content format. Specified by:

Specified by:
getContentFormat in interface Layout<String>
Overrides:
getContentFormat in class AbstractLayout<String>
Returns:
Map of content format keys supporting PatternLayout

toSerializable

public 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.

createPatternParser

public static PatternParser createPatternParser(Configuration config)
Create a PatternParser.

Parameters:
config - The Configuration.
Returns:
The PatternParser.

toString

public String toString()
Overrides:
toString in class Object

createLayout

@PluginFactory
public static PatternLayout createLayout(@PluginAttribute(value="pattern",defaultString="%m%n")
                                                       String pattern,
                                                       @PluginConfiguration
                                                       Configuration config,
                                                       @PluginElement(value="Replace")
                                                       RegexReplacement replace,
                                                       @PluginAttribute(value="charset",defaultString="UTF-8")
                                                       Charset charset,
                                                       @PluginAttribute(value="alwaysWriteExceptions",defaultBoolean=true)
                                                       boolean alwaysWriteExceptions,
                                                       @PluginAttribute(value="noConsoleNoAnsi",defaultBoolean=false)
                                                       boolean noConsoleNoAnsi,
                                                       @PluginAttribute(value="header")
                                                       String header,
                                                       @PluginAttribute(value="footer")
                                                       String footer)
Create a pattern layout.

Parameters:
pattern - The pattern. If not specified, defaults to DEFAULT_CONVERSION_PATTERN.
config - The Configuration. Some Converters require access to the Interpolator.
replace - A Regex replacement String.
charset - The character set.
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 codes
header - The footer to place at the top of the document, once.
footer - 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

newBuilder

@PluginBuilderFactory
public static PatternLayout.Builder newBuilder()
Creates a builder for a custom PatternLayout.

Returns:
a PatternLayout builder.


Copyright © 1999-2014 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.