Spring boot - number of backup log files restricted to 7

Ravindra Devadiga picture Ravindra Devadiga · Sep 21, 2016 · Viewed 20.9k times · Source

In our spring-boot project we are using slf4j for logging purpose. Below are configuration which we have added in application.properties file

logging.file=/opt/logs/my_log.log
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
logging.level.nl.yestelecom.boss=DEBUG
logging.level.com.github.isrsal.logging.LoggingFilter=DEBUG

It generates only 7 backup files (my_log.log.1, my_log.log.2 ..., my_log.log.7) with each file of size 10.5MB and after that logging is not happening at all.

Is there any way to change this behavior?

We looked into available properties of spring-boot but, didn't find anything. Any suggestion is appreciated.

Answer

DeepakV picture DeepakV · Sep 21, 2016

Spring-Boot only allows limited properties to be configured in its application.properties. See the list here.

The default (out-of-the-box) configuration that Spring-boot uses is defined in base.xml. See base.xml config here which includes this File appender

There are 2 ways to add extra configuration

  1. Add logback-spring.xml

If there is a logback configuration XML with name logback-spring.xml in project's classpath, it is picked up by Spring-Boot on initialization.

  1. Point to config file from application.properties

Within application.properties use following to point to your custom logback XML

logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback

Once you add the extra config using any of the above 2 steps, the rollover strategy can be mentioned within that custom XML like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
        </encoder>
    </appender>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%i</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy
        class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE"/>
    </root>
</configuration>