logback configuration
spring-boot logging
ANSI color logging
java logging
logback customization

How to configure logback in spring-boot for ANSI color feature?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Spring Boot already has built-in support for colorized console logging, but custom Logback configuration can disable it if you do not wire Boot's converters back in. The reliable setup is to use logback-spring.xml, include Boot's Logback defaults, and enable ANSI output for the terminal where the app runs.

How Spring Boot Adds Color

The key piece is the %clr conversion word used in log patterns. Spring Boot registers that converter and maps log levels to readable colors in the console. If you replace the default logging pattern with a plain Logback file and forget Boot's defaults, %clr may not work as expected.

That is why custom Boot projects should usually use logback-spring.xml instead of logback.xml. The -spring variant lets Spring Boot participate in Logback setup and provides access to Boot-specific features.

Minimal Working Configuration

Create src/main/resources/logback-spring.xml with Boot's defaults included:

xml
1<?xml version="1.0" encoding="UTF-8"?>
2<configuration>
3    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
4    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
5
6    <root level="INFO">
7        <appender-ref ref="CONSOLE"/>
8    </root>
9</configuration>

This is enough for many applications because Boot's console appender already uses a color-aware pattern.

If you want a custom pattern, define your own appender and keep %clr in the layout:

xml
1<?xml version="1.0" encoding="UTF-8"?>
2<configuration>
3    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
4
5    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
6        <encoder>
7            <pattern>
8                %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint}
9                %clr(${PID:- }){magenta}
10                %clr([%15.15thread]){faint}
11                %clr(%-5level)
12                %clr(%logger{36}){cyan}
13                %clr(:){faint}
14                %msg%n%wEx
15            </pattern>
16        </encoder>
17    </appender>
18
19    <root level="INFO">
20        <appender-ref ref="STDOUT"/>
21    </root>
22</configuration>

The %clr segments apply color to individual parts of the log line while keeping the pattern readable.

Enable ANSI Output In Spring Boot

Spring Boot also needs to know whether ANSI escape codes should be emitted. In application.properties:

properties
spring.output.ansi.enabled=ALWAYS

Use ALWAYS when you know the target terminal supports ANSI colors. DETECT is safer in mixed environments because Boot tries to decide automatically.

If you are shipping logs to files or systems that do not understand ANSI escape sequences, do not apply this console-oriented configuration there.

Customizing By Log Level

You can define different colors for different parts of the output. The %clr converter uses sensible defaults for log levels, but you can be explicit:

xml
1<pattern>
2    %clr(%d{HH:mm:ss.SSS}){faint}
3    %clr(%-5level){yellow}
4    %clr(%logger{30}){blue}
5    %msg%n
6</pattern>

In practice, most teams leave the level coloring to Boot defaults and only tune the rest of the pattern for readability.

Example Logger Output

With a properly configured console appender:

java
1import org.slf4j.Logger;
2import org.slf4j.LoggerFactory;
3import org.springframework.boot.CommandLineRunner;
4import org.springframework.stereotype.Component;
5
6@Component
7public class DemoRunner implements CommandLineRunner {
8    private static final Logger log = LoggerFactory.getLogger(DemoRunner.class);
9
10    @Override
11    public void run(String... args) {
12        log.info("Application started");
13        log.warn("Cache warmup is taking longer than expected");
14        log.error("Unable to connect to downstream service");
15    }
16}

The rendered console output will color the log levels and other pattern segments according to the %clr rules and terminal support.

Common Pitfalls

The most common mistake is putting the configuration in logback.xml and then expecting Spring Boot extensions to behave exactly the same. Use logback-spring.xml for Boot-aware logging features.

Another pitfall is forgetting to include Boot's defaults. If %clr is unknown or colors disappear after customization, check whether defaults.xml is included.

Developers also sometimes enable ANSI colors globally and then wonder why log files contain escape sequences. Keep colorized output for console appenders, not file appenders.

Finally, not every terminal or IDE console handles ANSI codes the same way. If colors do not appear, test the app in a normal shell first before assuming the Logback configuration is wrong.

Summary

  • Use logback-spring.xml for Spring Boot custom logging configuration.
  • Include Boot's Logback defaults so %clr and related converters are available.
  • Set spring.output.ansi.enabled=ALWAYS or DETECT depending on the environment.
  • Apply color through %clr(...) inside the console pattern.
  • Keep ANSI-colored output limited to terminals that support it.

Course illustration
Course illustration

All Rights Reserved.