Home > Spring > Spring Boot > Configure Logback in Spring-Boot Application for Logging

Configure Logback in Spring-Boot Application for Logging

In this article, we will learn how to configure logback in the spring boot application for logging and how to configure Logback with specifications including console and file output separately, and here we will also learn to use the rolling file policy to avoid generating large size log files and after a certain threshold size limit it will be back up in a date folder.

Best part of this approach is we will not be polluting our application.properties file. All the configuration will be written in the logback-spring.xml file. This file is placed inside the resources folder of spring boot application, parallel to other properties files.

logback-spring.xml

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

    <property name="LOGS" value="/tmp" />

    <appender name="Console"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/application.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable</Pattern>
        </encoder>

        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/${date:yyyy-MM}/application.log-%d{-dd-MMMM-yyyy}-%i.log.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <root level="debug">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <logger name="com.jkoder" level="debug" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

    <logger name="org.springframework" level="debug" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

</configuration>

We have defined /tmp as the base path of the logger file in the property tag, you can use different path based on your logging requirement.

Using the appender we have defined the Console configuration. Here we have defined the layout pattern using the color code and what to be printed in the log file.

Our output log file is using the RollingFile policy, which has been defined using the tag appender. In this policy we have defined the minimum size of the file, upon reaching the limit file will be moved to another folder in the gz(compressed) format, All of this configuration can be seen in this appender tag.

At the root level, we have configured the system to log everything at the INFO log level,, which has been defined using the root tag. Most of the time we comment on this tag as this may lead to an unnecessary amount of logging, which in most cases is not required.

For package-level logging, we have defined using the logger tag. Here in the above example, we have used com.jkoder and org.springframework, with log level debug (you can use other log levels based on your requirement like info, trace, etc.). If you want to include other packages like hibernate or any other packages, you will have to add those using the logger tag.