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:
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:
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:
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:
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:
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.xmlfor Spring Boot custom logging configuration. - Include Boot's Logback defaults so
%clrand related converters are available. - Set
spring.output.ansi.enabled=ALWAYSorDETECTdepending on the environment. - Apply color through
%clr(...)inside the console pattern. - Keep ANSI-colored output limited to terminals that support it.

