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

Hussain Pirosha picture Hussain Pirosha · Jan 8, 2015 · Viewed 19k times · Source

I have a spring-boot application running on windows. I have added jansi(v1.8) dependency in my project for enabling colour terminal output on windows.

Have included the logback configuration provided in spring-boot i.e. have added the following line in logback.xml.

<include resource="org/springframework/boot/logging/logback/base.xml" />

I am not clear as what to configure in logback.xml so that it log statements are coloured as per base.xml provided by spring-boot. Sorry, if is a really dumb question, I am a newbie on logback.

Thanks !

Answer

Andy Wilkinson picture Andy Wilkinson · Jan 12, 2015

This is described in the coloring section of the Logback documentation:

Grouping by parentheses as explained above allows coloring of sub-patterns. As of version 1.0.5, PatternLayout recognizes "%black", "%red", "%green","%yellow","%blue", "%magenta","%cyan", "%white", "%gray", "%boldRed","%boldGreen", "%boldYellow", "%boldBlue", "%boldMagenta", "%boldCyan", "%boldWhite" and "%highlight" as conversion words. These conversion words are intended to contain a sub-pattern. Any sub-pattern enclosed by a coloring word will be output in the specified colour.

There's also a sample configuration file that shows you how to use the conversion words:

<configuration debug="true">
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- On Windows machines setting withJansi to true enables ANSI
         color code interpretation by the Jansi library. This requires
         org.fusesource.jansi:jansi:1.8 on the class path.  Note that
         Unix-based operating systems such as Linux and Mac OS X
         support ANSI color codes by default. -->
    <withJansi>true</withJansi>
    <encoder>
      <pattern>[%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
    </encoder>
  </appender>
  <root level="DEBUG">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>