Spring Boot
IntelliJ
Console Logging
Color Output
Java Development

Getting Spring Boot color console logging working within Intellij?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Spring Boot is a powerful framework for developing Java-based applications. One of its attractive features is its built-in logging capability which outputs structured logs to the console. By default, these logs can be colorized, but getting those colorful logs out of Spring Boot and into your IntelliJ console can sometimes be a bit tricky. In this article, we will explore how to achieve color console logging within IntelliJ when using Spring Boot. This will make debugging and reading your logs a more enjoyable and efficient task.

Configuring Spring Boot for Colorized Console Output

Spring Boot uses the popular logging library Logback as the default logging mechanism. Logback provides ANSI color codes capabilities, which is what enables colored console output. However, there are certain prerequisites and configurations that must be set up to ensure it works correctly within IntelliJ.

Prerequisites

  1. Java Version: Ensure that you are using JDK 8 or later. Previous versions may not fully support colored console output.
  2. IntelliJ IDEA Version: The version of IntelliJ should be recent enough to support ANSI coloring natively in its terminals. Versions after IntelliJ IDEA 2018.3 generally have good support.

Steps to Enable Color Logging

  1. Update application.properties:
    Ensure that your src/main/resources/application.properties file contains the following property:
properties
    spring.output.ansi.enabled=ALWAYS

This property enforces Spring Boot to output ANSI color codes in logs regardless of the terminal environment, which is crucial for IntelliJ.

  1. Check IntelliJ Terminal Settings:
    IntelliJ IDEA’s terminal and console must be set to support ANSI coloring. Go to File -> Settings (or Preferences on macOS) -> Editor -> Color Scheme -> Console Colors, and ensure that the option Use color scheme font in console is enabled.
  2. Add ANSI Escape Sequences:
    If you're using a custom logging format in your logback-spring.xml, make sure you include patterns with ANSI escape sequences. Here is an example configuration:
xml
1    <configuration>
2        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
3            <encoder>
4                <pattern>%highlight(%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n)</pattern>
5            </encoder>
6        </appender>
7
8        <root level="INFO">
9            <appender-ref ref="CONSOLE" />
10        </root>
11    </configuration>

The %highlight pattern is particularly important as it is responsible for applying colors based on the log level.

Examining the Output

Once configured, run your Spring Boot application. The logs should now appear in color within the IntelliJ console:

  • INFO messages will generally appear in white or green.
  • WARN messages may appear in yellow.
  • ERROR messages will be displayed in red.

This is controlled by the Logback formatting as well as IntelliJ’s ANSI support.

Troubleshooting Common Issues

Despite the straightforward setup, issues can arise. Here are some common problems and their solutions:

  • Logs are not colorized:
    • Ensure spring.output.ansi.enabled=ALWAYS is set.
    • Double-check version compatibility for both IntelliJ and JDK.
  • Error with ANSI codes in Logs:
    • Ensure your IntelliJ console is set to handle ANSI escape codes, or update to a more recent version.

Summary

The following table summarizes key configuration points for enabling color logs in Spring Boot when using IntelliJ:

AttributeDescriptionRequired Action
Java VersionJDK 8 or later.Update Java if necessary.
IntelliJ Version2018.3 or later recommended.Update IntelliJ if necessary.
spring.output.ansi.enabled propertyEnables ANSI codes in logs.Set property to ALWAYS.
IntelliJ Terminal SettingsConfigure to support ANSI coloring.Enable Use color scheme font in console.
Logback ConfigurationUse %highlight in log patterns.Adjust logback-spring.xml.

In summary, leveraging color console logging in Spring Boot enhances the readability of logs, making it quicker and easier to identify issues and understand log output. With the correct configurations in both Spring Boot and IntelliJ IDEA, you can take full advantage of this feature, enabling more efficient debugging and development processes.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design