31

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 !

Hussain Pirosha
  • 1,358
  • 1
  • 11
  • 19

3 Answers3

42

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>
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 4
    you are right.But my question is in context with Spring Boot. If you see the base.xml that comes bundled with spring-boot already includes the configuration. How to activate is my question ? What do I need to set in my logback.xml ? – Hussain Pirosha Jan 12 '15 at 10:07
  • As far as I can tell that's harder than it should be. It doesn't appear to be possible to include `base.xml` and just enable colour-coded output. I've opened [an issue](https://github.com/spring-projects/spring-boot/issues/2331) so that we (the Spring Boot team) can investigate. – Andy Wilkinson Jan 12 '15 at 11:34
  • ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:18 - no applicable action for [withJansi], current ElementPath is [[configuration][appender][withJansi]] – Zon Jul 15 '21 at 20:47
32

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
acsadam0404
  • 2,711
  • 1
  • 26
  • 36
  • 6
    Not just set `spring.output.ansi.enabled=ALWAYS` but also need to use the `%clr` conversion word in your logging pattern. Example, if the logging pattern was previously `logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%n` now it could instead be entirely colored as `logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%n)` or to color just the log-level in console `logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} $clr(%-5level) - %msg%n` Took me hours to get that into my thick skull. – Net Dawg Sep 10 '21 at 00:39
1

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>
Rayon
  • 661
  • 8
  • 9