Colored logs in terminal like Spring Boot

As you probably know, Spring Boot has really sweet console logs by default:

console-logs

You can achieve pretty the same result in your CUBA applications. CUBA uses well known logback library as logging backend and its log settings can be easily customized.

Logging configuration is set in logback.xml file. You can find it in deploy/tomcat/conf/ directory on your development machine.

In the simplest case you just open this file and replace appender with name Console:

<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %highlight(%-5level) [%10thread] %cyan(%-40logger{36}) - %msg%n</pattern>
    </encoder>
</appender>

Here we apply %highlight and %cyan for coloring and %10, %-40 for char padding.

In case you are using Windows you have to add the following dependency to global module dependencies: org.fusesource.jansi:jansi:1.8 and specify the appender with the additional option withJansi:

<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <withJansi>true</withJansi>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %highlight(%-5level) [%10thread] %cyan(%-40logger{36}) - %msg%n</pattern>
    </encoder>
</appender>

After that your terminal will look like this:

tomcat-logs

Take a look at the full logback docs on layouts and coloring.

6 Likes