How to color logs in Logback

Very useful trick, that can make your logs more readable, especially in console.

In Logback config file (in my project its logback.xml) you have to enclose the required template in parentheses and add a color indication in front of it in the format “% cyan (…)”.

Here is an example for the console log output appender:

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{HH:mm:ss.SSS} %highlight(%-5level) %cyan(%class{50}.%M:%L) - %msg%n</pattern>
        </encoder>
    </appender>

Supported colors: «%black», «%red», «%green»,»%yellow»,»%blue», «%magenta»,»%cyan», «%white», «%gray», «%boldRed»,»%boldGreen», «%boldYellow», «%boldBlue», «%boldMagenta»»%boldCyan», «%boldWhite» and «%highlight».

As a result, we get this more visual output to the console:

Sources and additional information:

Leave A Comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked