Published on

LogBack Configuration

Authors

Spring boot comes by default with support for logBack.

I dig logBack as it is very flexible yet still fairly easy to configure and use. It also gives you very fine-grained control over how different packages and even classes log. For example, you could log everything at DEBUG level but the foo.bar package at INFO and the foo.bar.Buzz class at TRACE if you are running into serious issues and DEBUG doesn't cut it.

Below is an example of the sort of logBack setup I like to use:

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

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logFile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

            <!-- keep 30 days' worth of history capped at 1GB total size -->
            <maxHistory>30</maxHistory>
            <totalSizeCap>1GB</totalSizeCap>

        </rollingPolicy>

        <encoder>
            <pattern>%date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX' (UTC+0)'", UTC} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX' (UTC+0)'", UTC} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Change to DEBUG log protocol messages -->
    <logger name="org.web3j.protocol" level="INFO"/>
    <logger name="org.springframework.jms" level="DEBUG"/>
    <logger name="foo.bar" level="DEBUG"/>

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

This setup:

  • Rolls over logs every day or/and once the log file gets to a certain size
  • Outputs to a file and the console
  • Specifies clearly the date and time (more on this below)
  • Tells you the thread
  • Tells you the log level
  • Tells you the class being logged
  • And finally tells you the actual thing you want to log

This will log each line with a date that looks as follows:

2020-01-21T15:30:14,823Z (UTC+0) [main] INFO  foo.bar.config.AppConfig - ==================================================================================
2020-01-21T15:30:14,823Z (UTC+0) [main] INFO  foo.bar.config.AppConfig - Version hash: [13118af]

I generally log in this format as its easy at a glance to see:

  • What date you are looking at 2020-01-21 - none more "is this today's logs???"
  • The time to the millisecond T15:30:14,823 and in 24 hour time.
  • The time zone (UTC+0) just for sanity sake as you will always know the time relative to your timezone no matter any quirky server timezone config. This is literally a label but the config explicitly tells logBack to use UTC as the timezone.
    • This is also really useful when your app is deployed to multiple different timezones.
      • It is far easier to keep everything UTC+0 and add the offset yourself if need be.
      • You can now compare apples and apples when looking at the same app across different timezones.