Pattern Layout

PatternLayout is a customizable, efficient, garbage-free, and human-readable string generating layout using a user-provided pattern. It is analogous to String#format() with specialized directives on injecting certain properties of a LogEvent.

Pattern Layout is not intended for structural logging purposes. For production environments, you are strongly advised to use JSON Template Layout producing JSON output ready to be delivered to log ingestion systems such as Elasticsearch or Google Cloud Logging.

Usage

Pattern Layout is primarily configured using a conversion pattern referring to certain properties of a LogEvent. A conversion pattern is composed of literal text and format control expressions called conversion specifiers. For instance, given the following layout configuration

  • XML

  • JSON

  • YAML

  • Properties

Snippet from an example log4j2.xml
<PatternLayout pattern="%-5p [%t]: %m%n"/>
Snippet from an example log4j2.json
"PatternLayout": {
  "pattern": "%-5p [%t]: %m%n"
}
Snippet from an example log4j2.yaml
PatternLayout:
  pattern: "%-5p [%t]: %m%n"
Snippet from an example log4j2.properties
appender.0.layout.type = PatternLayout
appender.0.layout.pattern = %-5p [%t]: %m%n

then the following statements

LOGGER.debug("Message 1");
LOGGER.warn("Message 2");

will yield the output

DEBUG [main]: Message 1
WARN  [main]: Message 2

Any literal text, including \t, \n, \r\, and \f special characters, may be included in the conversion pattern. Use \\ to insert a single backslash into the output.

Each conversion specifier starts with a % character, and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data, e.g., category, priority, date, thread name. The format modifiers control such things as field width, padding, and left and right justification. Use %% to insert a single % into the output. Use %n to insert the line separator of the platform.

There is no explicit separator between text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character. In the example above the conversion specifier %-5p means the priority of the log event should be left justified to a width of five characters.

If the pattern string does not contain a specifier to handle a Throwable being logged, parsing of the pattern will act as if the %xEx specifier had been added to the end of the string. To suppress the formatting of the Throwable completely simply add %ex{0} as a specifier in the pattern string.

Configuration

This section explains how to configure Pattern Layout plugin element in a Log4j configuration file.

Plugin attributes

Pattern Layout plugin configuration accepts the following attributes:

charset

Type

Charset

Default value

The platform default

Charset used for encoding the produced JSON into bytes

pattern

Type

String

Default value

%m%n

A composite pattern string of one or more Pattern converters. pattern and patternSelector are mutually exclusive, that is, only one can be specified.

This attribute supports runtime property substitution using an event evaluation context.

If the provided pattern does not contain an exception converter and alwaysWriteExceptions is not disabled, an implicit %xEX is appended to the pattern.

patternSelector

Type

PatternSelector

A component that analyzes information in the LogEvent and determines which pattern should be used to format the event. patternSelector and pattern are mutually exclusive, that is, only one can be specified.

replace

Type

RegexReplacement

Allows portions of the resulting String to be replaced. If configured, the replace element must specify the regular expression to match and the substitution.

alwaysWriteExceptions

Type

boolean

Default value

true

If true and the user-provided pattern does not contain an exception converter, an implicit %xEX pattern is appended. This means that if you do not include a way to output exceptions in your pattern, the default exception formatter will be added to the end of the pattern. Setting this to false disables this behavior and allows you to exclude exceptions from your pattern output.

header

Type

String

The optional header to include at the top of each log file

Type

String

The optional footer to include at the bottom of each log file

disableAnsi

Type

boolean

Default value

false

If true, do not output ANSI escape codes

noConsoleNoAnsi

Type

boolean

Default value

false

If true and System.console() is null, do not output ANSI escape codes

Plugin elements

Pattern Layout plugin configuration accepts the following elements:

RegexReplacement

Allows portions of the resulting String to be replaced. This performs a function similar to the replace converter but applies to the whole message while the converter only applies to the String its pattern generates.

It supports following attributes:

regex

A Java-compliant regular expression to match the resulting string

replacement

The string to replace any matched substrings with

PatternSelector

Pattern Layout can be configured with a PatternSelector to allow it to choose a pattern to use based on attributes of the log event or other factors. A PatternSelector will normally be configured with a defaultPattern attribute, which is used when other criteria don’t match, and a set of PatternMatch elements that identify the various patterns that can be selected.

Predefined PatternSelectors are as follows:

LevelPatternSelector

The LevelPatternSelector selects patterns based on the level of the log event. Its configuration is similar to MarkerPatternSelector, with the difference that the key attribute of the PatternMatch element is matched against the log level associated with the log event.

MarkerPatternSelector

The MarkerPatternSelector selects patterns based on the marker included in the log event. If the marker in the log event is equal to or is an ancestor of the name specified on the key attribute of the PatternMatch element, then the pattern specified on that PatternMatch element will be used.

Below is a MarkerPatternSelector example switching from the [%-5level] %c{1.} %msg%n default pattern to [%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n, if the marker matches to FLOW:

  • XML

  • JSON

  • YAML

  • Properties

Snippet from an example log4j2.xml
<PatternLayout>
  <MarkerPatternSelector defaultPattern="[%-5level] %c{1.} %msg%n">
    <PatternMatch key="FLOW" pattern="[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"/>
  </MarkerPatternSelector>
</PatternLayout>
Snippet from an example log4j2.json
"PatternLayout": {
  "MarkerPatternSelector": {
    "defaultPattern": "[%-5level] %c{1.} %msg%n",
    "PatternMatch": [
      {
        "key": "FLOW",
        "pattern": "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"
      }
    ]
  }
}
Snippet from an example log4j2.yaml
PatternLayout:
  MarkerPatternSelector:
    defaultPattern: "%-5p [%t]: %m%n"
    PatternMatch:
      - key: "FLOW"
        pattern: "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"
Snippet from an example log4j2.properties
appender.0.layout.type = PatternLayout
appender.0.layout.patternSelector.type = MarkerPatternSelector
appender.0.layout.patternSelector.defaultPattern = %-5p [%t]: %m%n
appender.0.layout.patternSelector.patternMatch.0.type = PatternMatch
appender.0.layout.patternSelector.patternMatch.0.key = FLOW
appender.0.layout.patternSelector.patternMatch.0.pattern = [%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n
ScriptPatternSelector

The ScriptPatternSelector selects patterns by matching the output of a script execution against given PatternMatch elements.

Below is an example using a script determining NoLocation or Flow keywords from a log event and matching it against two PatternMatches to configure the effective pattern:

  • XML

  • JSON

  • YAML

  • Properties

Snippet from an example log4j2.xml
<PatternLayout>
  <ScriptPatternSelector defaultPattern="[%-5level] %c{1.} %C{1.}.%M.%L %msg%n">
    <Script name="BeanShellSelector" language="bsh"><![CDATA[
      if (logEvent.getLoggerName().equals("NoLocation")) {
        return "NoLocation";
      } else if (logEvent.getMarker() != null && logEvent.getMarker().isInstanceOf("FLOW")) {
        return "Flow";
      } else {
        return null;
      }]]>
    </Script>
    <PatternMatch key="NoLocation" pattern="[%-5level] %c{1.} %msg%n"/>
    <PatternMatch key="Flow" pattern="[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"/>
  </ScriptPatternSelector>
</PatternLayout>
Snippet from an example log4j2.json
"PatternLayout": {
  "ScriptPatternSelector": {
    "defaultPattern": "[%-5level] %c{1.} %msg%n",
    "Script": {
      "name": "BeanShellSelector",
      "language": "bsh",
      "scriptText": "if (logEvent.getLoggerName().equals(\"NoLocation\")) { return \"NoLocation\"; } else if (logEvent.getMarker() != null && logEvent.getMarker().isInstanceOf(\"FLOW\")) { return \"Flow\"; } else { return null; }"
    },
    "PatternMatch": [
      {
        "key": "NoLocation",
        "pattern": "[%-5level] %c{1.} %msg%n"
      },
      {
        "key": "Flow",
        "pattern": "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"
      }
    ]
  }
}
Snippet from an example log4j2.yaml
PatternLayout:
  ScriptPatternSelector:
    defaultPattern: "%-5p [%t]: %m%n"
    Script:
      name: "BeanShellSelector"
      language: "bsh"
      scriptText: |
        if (logEvent.getLoggerName().equals("NoLocation")) {
          return "NoLocation";
        } else if (logEvent.getMarker() != null && logEvent.getMarker().isInstanceOf("FLOW")) {
          return "Flow";
        } else {
          return null;
        }
    PatternMatch:
      - key: "NoLocation"
        pattern: "[%-5level] %c{1.} %msg%n"
      - key: "Flow"
        pattern: "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"
Snippet from an example log4j2.properties
appender.0.layout.type = PatternLayout
appender.0.layout.patternSelector.type = ScriptPatternSelector
appender.0.layout.patternSelector.defaultPattern = [%-5level] %c{1.} %C{1.}.%M.%L %msg%n
appender.0.layout.patternSelector.script.type = Script
appender.0.layout.patternSelector.script.name = BeanShellSelector
appender.0.layout.patternSelector.script.language = bsh
appender.0.layout.patternSelector.script.scriptText =\
if (logEvent.getLoggerName().equals("NoLocation")) {\
  return "NoLocation";\
} else if (logEvent.getMarker() != null && logEvent.getMarker().isInstanceOf("FLOW")) {\
  return "Flow";\
} else {\
  return null;\
}
appender.0.layout.patternSelector.0.type = PatternMatch
appender.0.layout.patternSelector.0.key = NoLocation
appender.0.layout.patternSelector.0.pattern = [%-5level] %c{1.} %msg%n
appender.0.layout.patternSelector.1.type = PatternMatch
appender.0.layout.patternSelector.1.key = Flow
appender.0.layout.patternSelector.1.pattern = [%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n

Pattern converters

The Pattern Layout conversion pattern is composed of literal text and format control expressions called conversion specifiers – refer to Usage for details. Plugins implementing PatternConverter are admitted to the pattern converter registry of Pattern Layout, and used to resolve the conversion specifiers.

The predefined set of pattern converters will be shared in the following sections. While doing so, their syntax will be documented in a certain notation. Consider the following example for the syntax of Date pattern converter:

%d{dateSpecifier}[{timezone}]

This means that

  • %d identifies the associated pattern converter

  • {dateSpecifier} indicates that the converter accepts a required dateSpecifier parameter

  • [{timezone}] indicates that the converter accepts an optional timezone parameter

If you want to have %d{something} literal in your pattern without matching for the actual %d pattern converter, you can escape the % as follows: %%d{something}.

Class

Outputs the fully qualified class name of the caller issuing the logging request

ClassNamePatternConverter specifier grammar
C{precision}
class{precision}

This conversion specifier can be optionally followed by a precision specifier that follows the same rules as the logger name converter.

Capturing the source location information to generate the class name of the caller is an expensive operation, and is not garbage-free. The logger name converter can generally be used as a zero-cost substitute. See this section of the layouts page for details.

Date

Outputs the date of the log event

DatePatternConverter specifier grammar
d{pattern}[{timezone}]
date{pattern}[{timezone}]

The date conversion specifier may be followed by a set of braces containing a date and time pattern string per SimpleDateFormat. The predefined named formats are:

Pattern Example output

%d{DEFAULT}

2012-11-02 14:34:02,123

%d{DEFAULT_MICROS}

2012-11-02 14:34:02,123456

%d{DEFAULT_NANOS}

2012-11-02 14:34:02,123456789

%d{ISO8601}

2012-11-02T14:34:02,781

%d{ISO8601_BASIC}

20121102T143402,781

%d{ISO8601_OFFSET_DATE_TIME_HH}

2012-11-02’T'14:34:02,781-07

%d{ISO8601_OFFSET_DATE_TIME_HHMM}

2012-11-02’T'14:34:02,781-0700

%d{ISO8601_OFFSET_DATE_TIME_HHCMM}

2012-11-02’T'14:34:02,781-07:00

%d{ABSOLUTE}

14:34:02,781

%d{ABSOLUTE_MICROS}

14:34:02,123456

%d{ABSOLUTE_NANOS}

14:34:02,123456789

%d{DATE}

02 Nov 2012 14:34:02,781

%d{COMPACT}

20121102143402781

%d{UNIX}

1351866842

%d{UNIX_MILLIS}

1351866842781

You can also use a set of braces containing a time zone id per java.util.TimeZone#getTimeZone(String). If no date format specifier is given then the DEFAULT format is used.

You can also define custom date formats, see following examples:

Pattern Example output

%d{HH:mm:ss,SSS}

14:34:02,123

%d{HH:mm:ss,nnnn} to %d{HH:mm:ss,nnnnnnnnn}

14:34:02,1234 to 14:34:02,123456789

%d{dd MMM yyyy HH:mm:ss,SSS}

02 Nov 2012 14:34:02,123

%d{dd MMM yyyy HH:mm:ss,nnnn} to %d{dd MMM yyyy HH:mm:ss,nnnnnnnnn}

02 Nov 2012 14:34:02,1234 to 02 Nov 2012 14:34:02,123456789

%d{HH:mm:ss}{GMT+0}

18:34:02

%d{UNIX} outputs the UNIX time in seconds. %d{UNIX_MILLIS} outputs the UNIX time in milliseconds. The UNIX time is the difference – in seconds for UNIX and in milliseconds for UNIX_MILLIS – between the current time and 1970-01-01 00:00:00 (UTC). While the time unit is milliseconds, the granularity depends on the platform. This is an efficient way to output the event time because only a conversion from long to String takes place, there is no Date formatting involved.

There is also limited support for timestamps more precise than milliseconds when running on Java 9 or later. Note that not all DateTimeFormatter formats are supported. Only timestamps in the formats mentioned in the table above may use the nano-of-second pattern letter n instead of the fraction-of-second pattern letter S.

Users may revert to a millisecond-precision clock when running on Java 9 by setting the log4j2.clock system property to SystemMillisClock.

Only named date formats (DEFAULT, ISO8601, UNIX, UNIX_MILLIS, etc.) are garbage-free.

Encode

Encodes and escapes special characters suitable for output in specific markup languages

EncodingPatternConverter specifier grammar
enc{pattern}{[HTML|XML|JSON|CRLF]}
encode{pattern}{[HTML|XML|JSON|CRLF]}

By default, this encodes for HTML if only one option is specified. The second option is used to specify which encoding format should be used.

A typical usage would encode the message (i.e., %enc{%m}), but the input could come from other locations as well (e.g., from a Thread Context entry: %enc{%mdc{key}}).

Using the HTML encoding format, the following characters are replaced:

Characters Replacement

\r and \n

Converted into string literals \r and \n, respectively

&<>"'/

Replaced with the corresponding HTML entity

Using the XML encoding format, this follows the escaping rules specified by the XML specification:

Characters Replacement

&<>"'

The corresponding XML entity

Using the JSON encoding format, this follows the escaping rules specified by RFC 4627 section 2.5:

Characters Replacement

U+0000 - U+001F

\u0000 - \u001F

Any other control characters

Encoded into its \uABCD equivalent escaped code point

"

\"

\

\\

If you are using JSON encoder in your conversion pattern, it is a strong indicator that you are trying to implement structured logging using Pattern Layout – please, don’t! Use JSON Template Layout instead.

Using the CRLF encoding format, the following characters are replaced:

Characters Replacement

\r and \n

Converted into literal strings \r and \n, respectively

End-of-batch

Outputs the EndOfBatch status of the log event as true or false

EndOfBatchPatternConverter specifier grammar
endOfBatch

Equals

Replaces occurrences of a string (test) with its replacement (substitution) in the string resulting from the evaluation of the pattern:

equals{pattern}{test}{substitution}
equalsIgnoreCase{pattern}{test}{substitution}

For example, %equals{[%marker]}{[]}\{} will replace [] strings produced by events without markers with an empty string.

The pattern can be arbitrarily complex and in particular can contain multiple conversion keywords.

Exception

Outputs the Throwable attached to the log event

ThrowablePatternConverter specifier grammar
ex|exception|throwable
  { "none"
  | "full"
  | depth
  | "short"
  | "short.className"
  | "short.fileName"
  | "short.lineNumber"
  | "short.methodName"
  | "short.message"
  | "short.localizedMessage"
  }
  {filters(package,package,...)}
  {suffix(pattern)}
  {separator(separator)}

By default this will output the full trace as one would normally find with a call to Throwable#printStackTrace().

You can follow the throwable conversion word with an option in the form %throwable{option}.

%throwable{short} outputs the first line of the Throwable.

%throwable{short.className} outputs the name of the class where the exception occurred.

%throwable{short.methodName} outputs the method name where the exception occurred.

%throwable{short.fileName} outputs the name of the class where the exception occurred.

%throwable{short.lineNumber} outputs the line number where the exception occurred.

%throwable{short.message} outputs the message.

%throwable{short.localizedMessage} outputs the localized message.

%throwable{n} outputs the first n lines of the stack trace.

Specifying %throwable{none} or %throwable{0} suppresses output of the exception.

Use {filters(packages)} where packages is a list of package names to suppress matching stack frames from stack traces.

Use {suffix(pattern)} to add the output of pattern at the end of each stack frame.

Use a {separator(…​)} as the end-of-line string, e.g., separator(\|). The default value is the line.separator system property, which is operating system dependent.

Exception converter is not garbage-free.

Exception (Extended)

The same as the %throwable conversion, but also includes class packaging information

ThrowablePatternConverter specifier grammar
xEx|xException|xThrowable
  { "none"
  | "full"
  | depth
  | "short"
  | "short.className"
  | "short.fileName"
  | "short.lineNumber"
  | "short.methodName"
  | "short.message"
  | "short.localizedMessage"
  }
  {filters(package,package,...)}
  {suffix(pattern)}
  {separator(separator)}

Different from the %throwable conversion, at the end of each stack element of the exception, a string containing the name of the JAR file that contains the class or the directory the class is located in and the Implementation-Version as found in that JAR’s manifest will be added. If the information is uncertain, then the class packaging data will be preceded by a ~ (tilde) character.

File

Outputs the file name where the logging request was issued

F
file

Capturing the source location information to generate the file name of the caller is an expensive operation, and is not garbage-free. See this section of the layouts page for details.

FQCN

Outputs the fully qualified class name of the logger

LoggerFqcnPatternConverter specifier grammar
fqcn

Highlight

Adds ANSI colors to the result of the enclosed pattern based on the current event’s logging level. Windows users should refer to ANSI styling on Windows.

HighlightConverter specifier grammar
highlight{pattern}{style}

The style parameter is a comma-separated list of the following directives:

Table 1. Style parameter syntax
Directive Description

<level>=<style_expression>

Formats all messages matching level <level> using the style provided by <style_expression>. See Style modifiers for the syntax of <style_expression>.

STYLE=default

Sets the default style, which is equivalent to the following sequence of directives: FATAL=bold red, ERROR=bold red, WARN=yellow, INFO=green, DEBUG=cyan, TRACE=black.

STYLE=logback

Applies the style used by Logback’s %highlight converter, which is equivalent to the following sequence of directives: FATAL=blink bold red, ERROR=bold red, WARN=red, INFO=blue, DEBUG=normal, TRACE=normal.

You can use the default colors with:

%highlight{%d [%t] %-5level: %msg%n%throwable}

You can override the default colors in the optional {style} option. For example:

%highlight{%d [%t] %-5level: %msg%n%throwable}{FATAL=white, ERROR=red, WARN=blue, INFO=black, DEBUG=green, TRACE=magenta}

You can highlight only a portion of the log event:

%d [%t] %highlight{%-5level: %msg%n%throwable}

You can style one part of the message and highlight the rest of the log event:

%style{%d [%t]}{black} %highlight{%-5level: %msg%n%throwable}

You can also use the STYLE key to use a predefined group of colors:

%highlight{%d [%t] %-5level: %msg%n%throwable}{STYLE=logback}

Level

Outputs the level of the log event

LevelPatternConverter specifier grammar
p|level{level=label, level=label, ...}
p|level{length=n}
p|level{lowerCase=true|false}

You provide a level name map in the form level=value, level=value, where the level is the name of the Level and value is the value that should be displayed instead of the name of the `Level. For example:

%level{WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace, INFO=Info}

Alternatively, for the compact-minded:

%level{WARN=W, DEBUG=D, ERROR=E, TRACE=T, INFO=I}

More succinctly, for the same result as above, you can define the length of the level label:

%level{length=1}

If the length is greater than a level name length, the layout uses the normal level name.

You can combine the two kinds of options:

%level{ERROR=Error, length=2}

This gives you the Error level name and all other level names of length 2.

Finally, you can output lower-case level names (the default is upper-case):

%level{lowerCase=true}

Line

Outputs the line number from where the log request was issued

L
line

Capturing the source location information to generate the line number of the caller is an expensive operation, and is not garbage-free. See this section of the layouts page for details.

Location

Outputs location information of the caller which generates the logging event

LocationPatternConverter specifier grammar
l
location

The location information depends on the JVM implementation, but it usually consists of the fully qualified name of the calling method followed by the callers' file name and line number.

Capturing the source location information of the caller is an expensive operation, and is not garbage-free. See this section of the layouts page for details.

Logger

Outputs the name of the logger that published the log event

LoggerPatternConverter specifier grammar
c{precision}
logger{precision}

By default, the layout prints the logger name in full.

A logger conversion specifier can be optionally followed by a precision specifier, which consists of a decimal integer, or a pattern starting with a decimal integer.

  • When the precision specifier is an integer value, it reduces the size of the logger name. If the number is positive, the layout prints the corresponding number of the rightmost logger name components. If negative, the layout removes the corresponding number of leftmost logger name components.

  • If the precision contains periods then the number before the first period identifies the length to be printed from items that precede tokens in the rest of the pattern. If the number after the first period is followed by an asterisk it indicates how many of the rightmost tokens will be printed in full.

  • If the precision contains any non-integer characters, then the layout abbreviates the name based on the pattern. If the precision integer is less than one, the layout still prints the right-most token in full.

See the table below for abbreviation examples:

Pattern Logger name Output

%c{1}

org.apache.commons.Foo

Foo

%c{2}

org.apache.commons.Foo

commons.Foo

%c{10}

org.apache.commons.Foo

org.apache.commons.Foo

%c{-1}

org.apache.commons.Foo

apache.commons.Foo

%c{-2}

org.apache.commons.Foo

commons.Foo

%c{-10}

org.apache.commons.Foo

org.apache.commons.Foo

%c{1.}

org.apache.commons.Foo

o.a.c.Foo

%c{1.1.~.~}

org.apache.commons.test.Foo

o.a...Foo

%c{.}

org.apache.commons.test.Foo

…​.Foo

%c{1.1.1.*}

org.apache.commons.test.Foo

o.a.c.test.Foo

%c{1.2.*}

org.apache.commons.test.Foo

o.a.c.test.Foo

%c{1.3.*}

org.apache.commons.test.Foo

o.a.commons.test.Foo

%c{1.8.*}

org.apache.commons.test.Foo

org.apache.commons.test.Foo

Marker

Outputs the marker, if one is present

marker
markerSimpleName

marker outputs the full name of the marker, including its parents. Whereas, markerSimpleName outputs the simple name of the marker without its parents.

Map

Outputs the entries in a MapMessage, if one is present in the event

MapPatternConverter specifier grammar
K{key}
map{key}
MAP{key}

The K conversion character can be followed by the key for the map placed between braces, as in %K{clientNumber}, where clientNumber is the key. The value of the map corresponding to the key will be output. If no additional sub-option is specified, then all map entries are output using a {{key1,val1},{key2,val2}} format.

Max. length

Outputs the result of evaluating the given pattern and truncating the result

MaxLengthConverter specifier grammar
maxLen{pattern}{length}
maxLength{pattern}{length}

If the length is greater than 20, then the output will contain a trailing ellipsis. If the provided length is invalid, a default value of 100 is used.

For instance, %maxLen{%p: %c{1} - %m%notEmpty{ ⇒%ex{short}}}{160} will be limited to 160 characters with a trailing ellipsis. %maxLen{%m}{20} will be limited to 20 characters and no trailing ellipsis.

Message

Outputs the message associated with the log event

MessagePatternConverter specifier grammar
m{lookups}{ansi}
msg{lookups}{ansi}
message{lookups}{ansi}

Add {ansi} to render messages with ANSI escape codes. Windows users should refer to ANSI styling on Windows.

The default syntax for embedded ANSI codes is:

@\|code(,code)* text\|@

For example, to render the message Hello in green, use:

@\|green Hello\|@

To render the message Hello in bold and red, use:

@\|bold,red Warning!\|@

You can also define custom style names in the configuration with the syntax:

%message{ansi}{StyleName=value(,value)*( StyleName=value(,value)*)*}%n

For example:

%message{ansi}{WarningStyle=red,bold KeyStyle=white ValueStyle=blue}%n

The call site can look like this:

logger.info("@\|KeyStyle {}\|@ = @\|ValueStyle {}\|@", entry.getKey(), entry.getValue());

Method

Outputs the method name where the logging request was issued

M
method

Capturing the source location information to generate the method name of the caller is an expensive operation, and is not garbage-free. See this section of the layouts page for details.

Nanoseconds

Outputs the result of System.nanoTime() at the time the log event was created

NanoTimePatternConverter specifier grammar
N
nano

Not empty

Outputs the result of evaluating the pattern, if and only if all variables in the pattern are not empty

variablesNotEmpty{pattern}
varsNotEmpty{pattern}
notEmpty{pattern}

For example:

%notEmpty{[%marker]}

Process ID

Outputs the process ID, if supported by the underlying platform

ProcessIdPatternConverter specifier grammar
pid{defaultValue}
processId{defaultValue}

An optional defaultValue may be specified to be shown, if the platform does not support process IDs.

Relative

Outputs the number of milliseconds elapsed since the JVM was started until the creation of the log event

r
relative

Repeat

Produces a string containing the requested number of instances of the specified string

RepeatPatternConverter specifier grammar
R{string}{count}
repeat{string}{count}

For example, %repeat{*}{2} will result in the string **.

Replace

Replaces occurrences of a regular expression (regex) with its replacement (substitution) in the string resulting from the evaluation of the pattern

RegexReplacementConverter specifier grammar
replace{pattern}{regex}{substitution}

For example, %replace{%msg}{\s}{} will remove all spaces contained in the event message.

The pattern can be arbitrarily complex and in particular, can contain multiple conversion keywords. For instance, %replace{%logger %msg}{\.}{/} will replace all dots in the logger or the message of the event with a forward slash.

Root exception

The same as the exception converter, but the stack trace is printed starting with the first exception in the causal chain that was thrown followed by each subsequent wrapping exception

rEx|rException|rThrowable
  {
    ["none" | "short" | "full" | depth]
    [,filters(package,package,...)]
    [,separator(separator)]
  }
  {ansi(
    Key=Value,Value,...
    Key=Value,Value,...
    ...)
  }
  {suffix(pattern)}

The throwable conversion specifier can be followed by an option in the form %rEx{short}, which will only output the first line of the Throwable, or %rEx{n}, where the first n lines of the stack trace will be printed.

Specifying %rEx{none} or %rEx{0} will suppress printing of the exception.

Use filters(packages), where packages is a list of package names to suppress matching stack frames from stack traces.

Use a separator string to separate the lines of a stack trace, e.g., separator(|). The default value is the line.separator system property, which is platform dependent.

Use rEx{suffix(pattern)} to add the output of pattern to the output only when there is a Throwable to print.

Sequence number

Includes a sequence number that will be incremented in every event

sn
sequenceNumber

The counter is a static variable, so will only be unique within applications that share the same converter class object.

Style

Use ANSI escape sequences to style the result of the enclosed pattern. The syntax of the style_expression parameter is described in Style modifiers. Windows users should also refer to ANSI styling on Windows.

StyleConverter specifier grammar
style{pattern}{style_expression}

For example:

%style{%d{ISO8601}}{black} %style{[%t]}{blue} %style{%-5level:}{yellow} %style{%msg%n%throwable}{green}

You can also combine styles:

%d %highlight{%p} %style{%logger}{bold cyan} %C{1.} %msg%n

You can also use % with a color like %black, %blue, %cyan, and so on. For example:

%black{%d{ISO8601}} %blue{[%t]} %yellow{%-5level:} %green{%msg%n%throwable}

Thread context stack

Outputs the Thread Context stack (aka. Nested Diagnostic Context or NDC) associated with the thread that generated the log event

NdcPatternConverter specifiers grammar
x
NDC

Thread context map

Outputs the Thread Context map (aka. Mapped Diagnostic Context or MDC) associated with the thread that generated the log event

MdcPatternConverter specifiers grammar
X{key[,key2...]}
mdc{key[,key2...]}
MDC{key[,key2...]}

The X conversion character can be followed by one or more keys for the map placed between braces, as in %X{clientNumber}, where clientNumber is the key. The value in the MDC corresponding to the key will be output.

If a list of keys is provided, such as %X{name, number}, then each key that is present in the thread context will be output using the format {name=val1, number=val2}. The key/value pairs will be printed in the order they appear in the list.

If no sub-options are specified then the entire contents of the MDC key-value pair set is output using a format {key1=val1, key2=val2}. The key/value pairs will be printed in sorted order.

Thread ID

Outputs the ID of the thread that generated the log event

ThreadIdPatternConverter specifiers grammar
T
tid
threadId

Thread name

Outputs the name of the thread that generated the log event

ThreadNamePatternConverter specifier grammar
t
tn
thread
threadName

Thread priority

Outputs the priority of the thread that generated the log event

tp
threadPriority

UUID

Includes either a random or a time-based UUID

UuidPatternConverter specifier grammar
u{RANDOM|TIME}
uuid{RANDOM|TIME}

The time-based UUID is a Type 1 UUID generated using the MAC address of each host To ensure uniqueness across multiple JVMs and/or class loaders on the same host, a random number between 0 and 16,384 will be associated with each instance of the UUID generator class, and included in each time-based UUID generated. See also log4j2.uuidSequence. Because time-based UUIDs contain the MAC address and timestamp, they should be used with care.

Format modifiers

By default, the relevant information is output as is. However, with the aid of format modifiers it is possible to change the minimum field width, the maximum field width, and justification.

The optional format modifier is placed between the percent sign and the conversion character.

The first optional format modifier is the left justification flag which is just the - (minus) character. Then comes the optional minimum field width modifier. This is a decimal constant that represents the minimum number of characters to output. If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify), but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated. To use zeros as the padding character prepend the minimum field width with a zero.

This behavior can be changed using the maximum field width modifier which is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end. For example, if the maximum field width is eight and the data item is ten characters long, then the first two characters of the data item are dropped. This behavior deviates from the String#format(), where truncation is done from the end.

Truncation from the end is possible by appending a minus character right after the period. In that case, if the maximum field width is eight and the data item is ten characters long, then the last two characters of the data item are dropped.

Below are various format modifier examples for the category conversion specifier.

Pattern Left justify Min. width Max. width Comment

%20c

false

20

none

Left pad with spaces if the category name is less than 20 characters long.

%-20c

true

20

none

Right pad with spaces if the category name is less than 20 characters long.

%.30c

N/A

none

30

Truncate from the beginning if the category name is longer than 30 characters.

%20.30c

false

20

30

Left pad with spaces if the category name is shorter than 20 characters. However, if the category name is longer than 30 characters, then truncate from the beginning.

%-20.30c

true

20

30

Right pad with spaces if the category name is shorter than 20 characters. However, if the category name is longer than 30 characters, then truncate from the beginning.

%-20.-30c

true

20

30

Right pad with spaces if the category name is shorter than 20 characters. However, if the category name is longer than 30 characters, then truncate from the end.

Style modifiers

Pattern Layout supports styling your text using a variety of ANSI escape sequence, which can be used through the %highlight and %style pattern converters.

The generic syntax of a style expression is a space-separated list of:

  • constants from the AnsiEscape class

  • or expressions of the form #rrggbb or BG_#rrggbb, were r, g and b are hexadecimal digits

In EBNF form the syntax of a style expression is:

<style_expression> ::= <style_expression> ( " " <style_expression> )*
<style_modifier> ::= "#" <hex> <hex> <hex> <hex> <hex> <hex>
                   | "bg_#" <hex> <hex> <hex> <hex> <hex> <hex>
                   | <keyword>
<hex> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7"
        | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f"
<keyword> ::= "normal" | "bold" | "dim" | "underline"
            | "blink" | "reverse" | "hidden"
            | "black" | "bg_black" | "bright_black" | "bg_bright_black"
            | "red" | "bg_red" | "bright_red" | "bg_bright_red"
            | "green" | "bg_green" | "bright_green" | "bg_bright_green"
            | "yellow" | "bg_yellow" | "bright_yellow" | "bg_bright_yellow"
            | "blue" | "bg_blue" | "bright_blue" | "bg_bright_blue"
            | "magenta" | "bg_magenta" | "bright_magenta" | "bg_bright_magenta"
            | "cyan" | "bg_cyan" | "bright_cyan" | "bg_bright_cyan"
            | "white" | "bg_white" | "bright_white" | "bg_bright_white"

For example, you can use underline blue bg_bright_yellow to specify a blue underlined text on a bright yellow background.

The style specifiers have the following effects (see Select Graphic Rendition for details):

normal

Reverts all parameters to their default value

bold

Increases the font weight or the color intensity

dim

Decreases the fond weight or the color intensity

underline

Underlines the text on some terminals

blink

Causes the text to blink

reverse

Swaps foreground and background colors

hidden

Hides the text

Colors

The color of the text or the background can be specified with the following style modifiers:

Table 2. Color table (8 or 16 color terminals)
Text color Background color Visual

8 color terminals

black

bg_black

red

bg_red

green

bg_green

yellow

bg_yellow

blue

bg_blue

magenta

bg_magenta

cyan

bg_cyan

white

bg_white

16 color terminals

bright_black

bg_bright_black

bright_red

bg_bright_red

bright_green

bg_bright_green

bright_yellow

bg_bright_yellow

bright_blue

bg_bright_blue

bright_magenta

bg_bright_magenta

bright_cyan

bg_bright_cyan

bright_white

bg_bright_white

If your terminal supports 24-bit colors, you can specify:

  • the text color using the #rrggbb syntax, e.g. #dc143c will color your text crimson,

  • the background color using the bg_#rrggbb syntax, e.g. bg_#87ceeb will use a sky blue background.

ANSI styling on Windows

ANSI escape sequences are supported natively on many platforms, but not by default on Windows. To enable ANSI support add the Jansi dependency to your application, and set the log4j2.skipJansi system property to false. This allows Log4j to use Jansi to add ANSI escape codes when writing to the console.

Before Log4j 2.10, Jansi was enabled by default. The fact that Jansi requires native code means that Jansi can only be loaded by a single class loader. For web applications, this means the Jansi jar has to be in the web container’s classpath. To avoid causing problems for web applications, Log4j no longer automatically tries to load Jansi without explicit configuration from Log4j 2.10 onward.

Garbage-free configuration

Pattern Layout with the following limited set of conversion patterns is garbage-free. Format modifiers to control such things as field width, padding, left, and right justification will not generate garbage.

Pattern Comment

%c{precision}
%logger{precision}

%d
%date

Only the predefined date formats (DEFAULT, ISO8601, UNIX, UNIX_MILLIS, etc.) are garbage-free

%enc{pattern}
%encode{pattern}

%equals{pattern}{test}{substitution}
%equalsIgnoreCase{pattern}{test}{substitution}

%highlight{pattern}{style}

Granted nested pattern is garbage-free

%K{key}
%map{key}
%MAP{key}

%m
%msg
%message

%marker

%markerSimpleName

%maxLen
%maxLength

%n

%N
%nano

%notEmpty{pattern}
%varsNotEmpty{pattern}
%variablesNotEmpty{pattern}

%p
%level

%r
%relative

%sn
%sequenceNumber

%style{pattern}{ANSI style}

%T
%tid
%threadId

%t
%tn
%thread
%threadName

%tp

,key2…​\}
%mdc{key[,key2…​\]}
%MDC{key[,key2…​\]}

literal text

Garbage-free, but care is needed for Property substitution, including Lookups

Patterns containing regular expressions and location information are not garbage-free.

Property substitution

Property substitutions (e.g., ${myProperty}), including lookups (e.g., ${java:version}, ${env:USER}, ${date:MM-dd-yyyy}) are supported, but extra care needs to be taken. We strongly advise you to carefully read the configuration manual before using them.

Lookups are intended as a very generic, convenience utility to perform string interpolation for, in particular, configuration files and components (e.g., layouts) lacking this mechanism. Pattern Layout has a rich converter collection, and you should always prefer it whenever possible over lookups.

Which converters can I use to replace lookups?

Extending

Pattern Layout relies on the Log4j plugin system to compose the features it provides. This makes it possible for users to extend the plugin-based feature set as they see fit. As of this moment, only extending pattern converters is supported. Following sections cover how to extend these in detail.

While existing features should address most common use cases, you might find yourself needing to implement a custom one. If this is the case, we really appreciate it if you can share your use case in a user support channel.

Plugin preliminaries

Log4j plugin system is the de facto extension mechanism embraced by various Log4j components. Plugins provide extension points to components, that can be used to implement new features, without modifying the original component. It is analogous to a dependency injection framework, but curated for Log4j-specific needs.

In a nutshell, you annotate your classes with @Plugin and their (static) factory methods with @PluginFactory. Last, you inform the Log4j plugin system to discover these custom classes. This is done using running the PluginProcessor annotation processor while building your project. Refer to Plugins for details.

Pattern converters

Plugins implementing PatternConverter are admitted to the pattern converter registry of Pattern Layout, and used to resolve the conversion specifiers. You can leverage this mechanism to introduce your custom pattern converters next to the predefined ones.

A PatternConverter must first declare itself as a plugin using the standard @Plugin annotation, and the @Namespace annotation with the value Converter. Furthermore, the converter must also specify the @ConverterKeys annotation to define the conversion specifiers, which will preceded by a % character when used in a pattern.

Unlike most other plugins, pattern converters do not use a @PluginFactory. Instead, each converter is required to provide a static newInstance(String[]) factory method. The String[] argument denots the the values that are specified within the curly braces that following the conversion specifier.

Refer to following sources for simple examples:

Performance

Great effort has been put into the efficiency of Pattern Layout. To get the most out of it, mind the following checklist: