Logback Pattern Syntax Quick Reference

Common Conversion Specifiers

Date and Time

  • %d{pattern}: Outputs the current timestamp. For example, %d{yyyy-MM-dd HH:mm:ss.SSS} yields 2024-07-11 15:34:55.123.

Log Level

  • %level or %p: Renders the log severity (e.g., INFO, DEBUG, WARN, ERROR).

Message and Source Context

  • %msg or %m: The actual log message.
  • %C or %class: Fully qualified class name.
  • %M or %method: Method name where the log was issued.
  • %L or %line: Source code line number.
  • %F or %file: Filename (e.g., Example.java).

Thread Information

  • %thread or %t: Name of the thread that generated the log event.

Logger Name

  • %logger{length} or %c{length}: Shortens the logger name to the specified maximum length by abbreviating package segments from the left if necessary.

Process ID

  • ${PID}: Inserts the process identifier. Must be set via System.setProperty("PID", "...").

Miscellaneous

  • %n: Platform-specific newline character.
  • %r: Milliseconds elapsed since application startup.
  • %property{key}: Resolves a system or configuration property (e.g., %property{os.name}).
  • %ex or %exception: Prints exception stack trace; %ex{5} limits output to 5 lines.
  • %nopex: Suppresses exception output.
  • %caller: Shows caller details in the format at pkg.Class.method(File.java:line).
  • %replace(pattern){regex, replacement}: Applies regex-based substitution (e.g., masking passwords: %replace(%msg){'password=\w+', 'password=*****'}).

Example Layout

A commonly used pattern:

%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %-40.40logger{39} : %msg%n%ex{5}

Breakdown:

  1. %d{...}: Timestamp with millisecond precision.
  2. %5p: Right-aligned log level padded to 5 characters (e.g., INFO).
  3. ${PID}: Process ID injected at runtime.
  4. [%15.15t]: Thread name, right-aligned, fixed width of 15 chars.
  5. %-40.40logger{39}: Logger name, left-aligned, max 40 chars wide; abbreviated if longer than 39 characters.
  6. %msg: Log payload.
  7. %n: Newline.
  8. %ex{5}: Exception stack trace limited to 5 lines.

Sample logback.xml Configuration

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="5 seconds">

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

    <property name="LOG_HOME" value="logs"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx</pattern>
            <charset>utf-8</charset>
        </encoder>
    </appender>

    <appender name="ARCHIVE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/latest.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd,aux}/%d{yyyy-MM-dd-HH}.log</fileNamePattern>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %-40.40logger{39} : %msg%n%ex{5}</pattern>
            <charset>utf-8</charset>
        </encoder>
    </appender>

    <logger name="top.meethigher.snipurl.utils.GlobalDecorator" level="DEBUG"/>
    <logger name="org.hibernate" level="WARN"/>

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ARCHIVE"/>
    </root>
</configuration>

In Spring Boot, specify this configuration via application.yml:

logging:
  config: file:logback-temp.xml

Tags: logback java-logging spring-boot

Posted on Sun, 09 Aug 2026 16:54:34 +0000 by MishaPappa