Skip to content

Log4j2 Pattern Layout

In Log4j2, the Pattern Layout is a configurable component used within appenders to define the format of log output.^[600-developer-java-3-party-java-log4j2.md] It works by interpreting a string of text and conversion characters (the pattern) to structure log events into a readable or machine-parsable string format, such as line-separator delimited text^[600-developer-java-3-party-java-log4j2.md] or JSON^[600-developer-java-3-party-java-log4j2.md].

A key feature of the Pattern Layout is its support for lookup references, allowing patterns to be defined as global properties and reused across different appenders^[600-developer-java-3-party-java-log4j2.md].

Configuration

Defining the Pattern

Patterns are typically defined in the configuration file (e.g., log4j2.xml) within the <Properties> section^[600-developer-java-3-party-java-log4j2.md]. This allows a single format string to be applied to multiple logging destinations.

A common pattern definition might look like this:

<Properties>
    <Property name="LOG_PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</Property>
</Properties>
^[600-developer-java-3-party-java-log4j2.md]

Applying the Pattern

Once defined, the pattern is applied to an appender (such as Console or RollingFile) using the <PatternLayout> tag and referencing the property variable^[600-developer-java-3-party-java-log4j2.md].

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="${LOG_PATTERN}"/>
    </Console>

    <RollingFile name="main" fileName="C:/logs/main.log">
        <PatternLayout pattern="${LOG_PATTERN}"/>
        <!-- Policies and Strategy configurations -->
    </RollingFile>
</Appenders>
^[600-developer-java-3-party-java-log4j2.md]

Pattern Syntax

The pattern string consists of format specifiers that act as placeholders for log event data^[600-developer-java-3-party-java-log4j2.md]. Common conversion words used in the pattern include:

  • %d{...}: Used to output the date of the log event^[600-developer-java-3-party-java-log4j2.md].
  • %p or %-5level: Outputs the level of the logging event (e.g., INFO, ERROR)^[600-developer-java-3-party-java-log4j2.md]. The -5 modifier aligns the text to a minimum width of 5 characters.
  • %t: Outputs the name of the thread that generated the logging event^[600-developer-java-3-party-java-log4j2.md].
  • %c{...}: Outputs the category of the logging event (usually the logger name)^[600-developer-java-3-party-java-log4j2.md]. The {1} precision modifier typically outputs the last part of the class name.
  • %m or %msg: Outputs the application-supplied message associated with the log event^[600-developer-java-3-party-java-log4j2.md].
  • %n: Outputs the line separator specific to the platform^[600-developer-java-3-party-java-log4j2.md].
  • %X or MDC**: Outputs the thread context stack (key-value pairs)^[600-developer-java-3-party-java-log4j2.md].
  • %ex or exception**: Outputs the throwable information attached to the log event^[600-developer-java-3-party-java-log4j2.md].

JSON and YAML Layouts

While Pattern Layout is the standard for text-based formatting, Log4j2 also supports specific layouts for structured data. For JSON or YAML outputs, it is recommended to use JsonLayout or YamlLayout rather than a custom Pattern Layout^[600-developer-java-3-party-java-log4j2.md].

  • [[Log4j2 XML Configuration]]
  • [[Logging Append]]
  • [[Structured Logging]]

Sources

  • 600-developer-java-3-party-java-log4j2.md