Hussain Pirosha
Hussain Pirosha

Reputation: 1376

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

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 !

Upvotes: 32

Views: 35393

Answers (3)

Rayon
Rayon

Reputation: 902

here is a sample config you can use.

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%t]){faint} %clr(%-40.40logger{39}.%M){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
    <!--<property name="CONSOLE_LOG_PATTERN" value="%d %p [%c{1}] - %m%n"/>-->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <appender name="ROLLING_APPENDER"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <!--<file>C:/tmp/var/demo/demo-app.log</file>-->
        <!--<file>/var/log/mds/demo-app.log</file>-->
        <file>${LOG_FOLDER}/demo-app.log</file>
        <rollingPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>
                ${LOG_FOLDER}/demo-app.log.%d{yyyy-MM-dd}.%i.log.zip
            </fileNamePattern>
            <!--<fileNamePattern>
                C:/tmp/var/demo-app.log.%d{yyyy-MM-dd}.%i.log.zip
            </fileNamePattern>-->
            <maxFileSize>100MB</maxFileSize>
            <maxHistory>20</maxHistory>
            <totalSizeCap>1000MB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="com.hsbc.app.demo" additivity="false" level="debug">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ROLLING_APPENDER"/>
    </logger>
    <logger name="org.springframework" level="INFO"/>

    <root level="info">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ROLLING_APPENDER"/>
    </root>
</configuration>

Upvotes: 1

acsadam0404
acsadam0404

Reputation: 2831

Spring boot added support for this:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-logging-color-coded-output

Just set

spring.output.ansi.enabled=always

Upvotes: 34

Andy Wilkinson
Andy Wilkinson

Reputation: 116111

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>

Upvotes: 48

Related Questions