reinhardpoetz.com :: articles

Enhancing Java Console Logs: A Practical Guide to Log4j2 Pattern Layout

Learn how to use Log4j2 for clear, styled console logs in Java applications, enhancing readability and efficiency in development
:: published on

At development time, I always watch the application logs. Spring Boot demonstrates that having nicely formatted and styled logs helps make them more readable.

Spring Boot using Logback

Since I started developing an application that does not use Spring Boot or Logback, I’ve needed something similar with Log4j2. Here is the pattern layout:

<Console name="Console" target="SYSTEM_OUT">
  <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue, TRACE=cyan} %style{%processId}{magenta} --- [%15.15t] %style{%-40.40c{1.}}{cyan} : %m %replace{%style{%X}{magenta}}{\{\}}{}%n" />
</Console>

Log4j2

The Log4j2 documentation contains all the details about the pattern layout.