/logback-colorizer

A customizable log colorizer for logback, based on log levels.

Primary LanguageJavaApache License 2.0Apache-2.0

logback-colorizer

v1.0.1

Build Status

Branch/Tag Status
v1.0.1 Build Status
master Build Status
dev Build Status

Overview

logback-colorizer is a customizable log colorizer for logback, based on log levels. This extension is far more customizable than the built-in coloring provided using highlight keyword of logback.

Works with any of the logback implementations such as logback-classic, logback-android.

Quick Start

  1. Add logback-colorizer to your project's class path.
  2. Configure the colors for different log levels using the COLORIZER_COLORS property in logback.xml.
  3. Choose any conversionWord as the keyword for applying colors, and add a new conversionRule. Set the converterClass as org.tuxdude.logback.extensions.LogColorizer in logback.xml.
  4. Use the previously chosen conversionWord in the pattern of your appender's encoder in logback.xml.

Example logback.xml

<configuration>
    <property scope="context" name="COLORIZER_COLORS" value="boldred@white,yellow@black,green@,blue@,cyan@" />
    <conversionRule conversionWord="colorize" converterClass="org.tuxdude.logback.extensions.LogColorizer" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %colorize(%msg)%n</pattern>
        </encoder>
    </appender>

    <root level="TRACE">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Download

  • Gradle

    • Add the following dependency in the project's build.gradle
    dependencies {
        ...
        ...
        compile 'org.tuxdude.logback.extensions:logback-colorizer:1.0.1'
        ...
        ...
    }
  • Maven

    • Add the following dependency in the project's pom.xml
    <dependencies>
        ...
        ...
        <dependency>
            <groupId>org.tuxdude.logback.extensions</groupId>
            <artifactId>logback-colorizer</artifactId>
            <version>1.0.1</version>
            <type>jar</type>
        </dependency>
        ...
        ...
    </dependencies>
  • Manually

Available Colors

Foreground and background colors can be set independently for each log level. The following colors are available:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

In addition a bold prefix is also supported with each color, for example boldred.

Choosing the colors for each log level

Specify the colors for each log level using the COLORIZER_COLORS property in logback.xml. It takes a comma-separated list of 5 color configurations, one for each log level.

Example:

<property scope="context" name="COLORIZER_COLORS" value="boldred@white,yellow@black,green@,blue@,cyan@" />

The order of specifying the log colors for different log levels is:

Error,Warn,Info,Debug,Trace

Each color configuration specifies a foreground and a background color:

foreground@background

If a color is left blank (no whitespaces), then the log color is left unchanged.

  • Example color configuration, which sets the foreground color to white and background color to boldblue.

    white@boldblue

  • Example color configuration, which sets the foreground color to magenta and leaves the background color unchanged:

    magenta@

  • Example color configuration, which sets the background color to boldgreen and leaves the foreground color unchanged:

    @boldgreen

  • Example color configuration, which leaves both the foreground and background colors untouched:

    @

Some valid example properties:

<property scope="context" name="COLORIZER_COLORS" value="red@,yellow@,green@,blue@,cyan@" />
<property scope="context" name="COLORIZER_COLORS" value="@red,@yellow,@green,@blue,@cyan" />
<property scope="context" name="COLORIZER_COLORS" value="red@white,yellow@black,@,@,@" />
<property scope="context" name="COLORIZER_COLORS" value="white@magenta,boldyellow@black,green@white,boldblue@,@" />

Conversion rule and pattern

The final part of configuring the logback-colorizer is to specify a custom conversion rule and use this in the encoder pattern.

Define a conversionRule using a custom conversionWord for LogColorizer. Use this custom word in the pattern to enable colorization. Only the part of the pattern which is within the parantheses (...) gets colorized, and can be used numerous times within the same pattern.

  • An example using colorize as the keyword to enable colorization:
<conversionRule conversionWord="colorize" converterClass="org.tuxdude.logback.extensions.LogColorizer" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %colorize(%msg)%n</pattern>
    </encoder>
</appender>
  • An example using github as the keyword to enable colorization:
<conversionRule conversionWord="github" converterClass="org.tuxdude.logback.extensions.LogColorizer" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %github([%thread]) %-5level %logger{36} - %github(%msg)%n</pattern>
    </encoder>
</appender>
  • An example using rainbow as the keyword to enable colorization:
<conversionRule conversionWord="rainbow" converterClass="org.tuxdude.logback.extensions.LogColorizer" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %rainbow([%thread]) %-5level %rainbow(%logger{36}) - %rainbow(%msg)%n</pattern>
    </encoder>
</appender>