Enable Colors For Logs With Logback Spring

Overview

Checking log without color is hard. Enable color for your log messages would make things a bit easier. I’ll show you how to do that with logback in spring.

Configuration

First of all, you need to create a file name logback-spring.xml under your resource folder. Here is my file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
    <property name="LOG_HOME" value="${LOG_HOME:-/opt/vp3/log}" />
    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>

    <!-- Console Appender -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %green([%thread]) %highlight(%-5level) %cyan(%logger{36}) - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Rolling File Appender -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/vp3-application.log</file>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %d{nnnnnnnnn} %d{Z} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- Daily rollover with a max file size -->
            <fileNamePattern>${LOG_HOME}/application-%d{yyyy-MM-dd_HH}.%i.log.gz</fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <maxHistory>30</maxHistory>
            <totalSizeCap>5GB</totalSizeCap>
            <!-- Prevent empty archive files -->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
    </appender>

    <logger name="com.datmt.logging" level="DEBUG" additivity="false">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </logger>

</configuration>

Here is my log configuration with rolling file appender and compression.

However, for color logging, you only need to pay attention to line 11 to 13.

Pattern Breakdown

  • %d{yyyy-MM-dd HH:mm:ss.SSS}
    • What it is: The timestamp converter.
    • What it does: Prints the date and time the log event occurred, formatted as year-month-day hour:minute:second.millisecond.
    • Example: 2025-08-23 07:12:00.123
  • %green([%thread])
    • What it is: A color converter applied to the thread name.
    • What it does: Prints the name of the execution thread that created the log (e.g., main, http-nio-8080-exec-1) inside square brackets and colors the text green. This converter is typically provided by Spring Boot.
    • Example: [main]
  • %highlight(%-5level)
    • What it is: The highlighting converter for the log level.
    • What it does: Prints the log level (INFO, DEBUG, WARN, ERROR) and colors it based on its severity (e.g., ERROR becomes red, WARN yellow, INFO blue or green). The %-5 part ensures the text is always 5 characters wide by adding spaces, which neatly aligns your logs.
    • Example: INFO or ERROR
  • %cyan(%logger{36})
    • What it is: A color converter for the logger name.
    • What it does: Prints the name of the logger (usually the class name that generated the log) and colors it cyan. The {36} abbreviates the name if it’s longer than 36 characters to keep lines from getting too long.
    • Example: c.e.m.service.AuthenticationService
  • -
    • What it is: A literal hyphen character.
    • What it does: Acts as a simple visual separator.
  • %msg%n
    • What it is: The message and newline converters.
    • What it does: %msg prints the actual log message you wrote in your code. %n adds a line break, so the next log appears on a new line.
    • Example: User login successful for 'admin'

Result

On windows (intellij)

Logging with color with logback, spring on windows

On Linux server (viewing from windows)

Logging with color with logback, spring on linux

Conclusion

In this post, I’ve shown you how to enable color for spring boot application. Hopefully this will make your life easier as a log reader.

Leave a Comment