/ #Java #Spring Boot 

Spring Boot Output Colors

Learn how to enable Spring Boot’s color-coded output in the terminal.

Spring Boot supports colors in the output to improve readability, like in the example below:

The following table describes the default mapping of log levels to colors:

Level Color
FATAL Red
ERROR Red
WARN Yellow
INFO Green
DEBUG Green
TRACE Green

The terminal has to support ANSI for this to work. Most Linux terminals already support and have it enabled by default but if you are on a macOS you will have to export an environment variable to enable it. There are many ways of doing this, like, for example, typing this command on the terminal:

export CLICOLOR=1

However, this will only work during the current session which means that if you close the terminal the colors will not be enabled next time you open it. To make it permanent, add that command to your profile script (that would be .bash_profile or .bashrc in your home directory if you are using Bash).

Enable Spring Boot color output

The property that controls the colored output is spring.output.ansi.enabled. You can set it to one of the supported values, which are:

ALWAYS: Enable ANSI-colored output.

DETECT: Try to detect whether ANSI coloring capabilities are available.

NEVER: Disable ANSI-colored output.

By default, Spring Boot sets this value to DETECT, but some terminals won’t display the color coded output unless this property is set to ALWAYS.

Add this line to the application.properties (by default located at src/main/resources) file to enable the colored output:

spring.output.ansi.enabled=ALWAYS

If you are using YAML for the application properties (using the file application.yml), you should add the following lines to the file:

spring:
  output:
    ansi:
      enabled: ALWAYS

Making it global

When setting the color output settings in the application files, the changes will be enabled only for the project which the application file belongs to. If you want to make these changes global, Spring Boot recognizes an environment variable to control the ANSI output. Just add the following line to your profile script to make these changes global:

export SPRING_OUTPUT_ANSI_ENABLED=ALWAYS

To know more, check the Spring Boot docs.


Cover picture by Paola Galimberti via Unsplash