Log4j2 PatternLayout Cheatsheet

In Log4j2 for Kotlin I’ve showed you how to configure a basic logger with log4j2.

If you use a PatternLayout like this

appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n

you can customize the appearance of the output.

eg.

appender.console.layout.pattern = %d{yyyy-mm-dd-HH:mm:ss.SSS} %-4p %C{1}.%M [%t] - %m%n%ex

Parameter

Parameter Meaning
%m The log message
%n line break
%d timestamp
%p priority
%t thread
%C class

Timestamps

 

%d{yyyy-mm-dd-HH:mm:ss.SSS}
2021-50-27-10:50:32.701
Parameter Meaning
yyyy year in four digits
MM

MMM

month in two digits

Name of month three characters wide

dd day
HH hour
mm minutes
ss seconds
SSS milliseconds