Logging configuration for Spring Boot (logback)

Logging configuration for Spring Boot (logback)

logWhen you start a project, you usually do not care about logging. Necessary information is printed to the console by default. That is usually enough for development but when you want to go with it to production, logging has to be configured. There are a few Java libraries that can handle logging like java.util.logging, Logback, Log4j, Jakarta Commons Logging. When you create a project based on Spring Boot Web Starter, Logback is added by default.

If you do nothing, Logback will print information to the console but not to a log file. Fortunately, configuring it is very easy. All you need to do is to create logback.xml and add it to the classpath. In my case it is <project path>/src/main/resources/.

Here is a sample logback.xml.

<configuration>
    <
include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <
property name="GENERAL_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
    <
appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <
file>c:\\Program Files\\Apache\\apache-tomcat\\logs\\salon.log</file>
        <
rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
           
<fileNamePattern>salon.%d{yyyy-MM-dd}.log</fileNamePattern>
           
<maxHistory>30</maxHistory>
            <
totalSizeCap>1GB</totalSizeCap>
        </
rollingPolicy>
        <
encoder>
            <
pattern>${GENERAL_LOG_PATTERN}}</pattern>
        </
encoder>
    </
appender>
    <
appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <
encoder>
            <
pattern>${GENERAL_LOG_PATTERN}</pattern>
        </
encoder>
    </
appender>
    <
root level="INFO">
        <
appender-ref ref="STDOUT"/>
        <
appender-ref ref="FILE"/>
    </
root>
</
configuration>

The property tag defines how message entries will be logged - timestamp format, where severity is placed etc. Two appenders are configured:

  • STDOUT which is responsible for printing messages to the console,
  • FILE which defines logging to files.

The file section defines a path to the log file. It should refer to a directory that already exists and the application server has access to it.

The rolling policy used in the configuration file sets time based rolling policy which means that the log file is archived based on time. Inside the tag are details defined like fileNamePattern. All entries from the same day are stored in the same file (salon.%d{yyyy-MM-dd}.log. It would be different if the date format contained also an hour not only a year, a month and a date.
MaxHistory option is set to 30 so only 30 last archived files are stored unless it exceeds 1 GB of disk space usage (totalSizeCap).

It is not all. One of the most fundamental logging setting is a level. It is defined in the root section. INFO level is applied to both STDOUT and FILE appenders.

To test this configuration, you should build a package and deploy it to an application server like Tomcat. More information about Spring Boot application with Tomcat can be found in Spring Boot application deployment on standalone Tomcat.

A little off-topic but here is a hint on writing clean code with logging related - Use the built-in formatting to construct this argument.

If you like what I do, consider buying me a coffee :)

Buy me a coffeeBuy me a coffee