springboot-Rolling log files by size or by date in springboot apps
1. The purpose of this post
I would demo how to roll the log files by date or by size in springboot apps.
2. Environments
- springboot 1.x and 2.x
3. Rolling log files in springboot apps
3.1 What’s the default logging configurations in spring?
By default, springboot use logback as the default logger, but there are some limitation if you only use application.properties to configure the logging:
logging.level.=ERROR # this is the root logging level for all packages.
logging.level.com.test.sb1jt=DEBUG # this is the project-specific package logging level configuration
logging.file=springboot-logger.log # this is the logging target file
You can see that, we can configure the root logging level and package logging level, but what should we do if we want to roll the logging files by date or by size?
3.2 Add logback configuration file to your project
When a file in the springboot classpath has one of the following names, Spring Boot will automatically load it over the default configuration:
- logback-spring.xml
- logback.xml
- logback-spring.groovy
- logback.groovy
And the logback-spring.xml is recommended.
3.3 Demo the rolling log file by size
We can define the rolling policy in src/main/res/logback-spring.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- All log files located in logs file of the project -->
<property name="LOGS" value="./logs" />
<!-- Define the console log format -->
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFileBySize" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/spring-boot-loggerbysize.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOGS}/spring-boot-loggerbysize-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each file should be at most 1MB, keep 6 days worth of history, but at most 3M -->
<maxFileSize>1MB</maxFileSize>
<maxHistory>6</maxHistory>
<totalSizeCap>3MB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
</appender>
<!-- LOG everything at error level -->
<root level="error">
<appender-ref ref="RollingFileByDate" />
<appender-ref ref="Console" />
</root>
<!-- LOG "com.test*" at TRACE level -->
<logger name="com.test" level="trace" additivity="false">
<appender-ref ref="RollingFileByDate" />
<appender-ref ref="Console" />
</logger>
</configuration>
Then you can test by running a infinite loop which print a lot of logs. Then you would get these files:
-rw-r--r-- 1 zzz staff 1.0M May 25 18:21 spring-boot-loggerbysize-2019-05-25.0.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:21 spring-boot-loggerbysize-2019-05-25.1.log
-rw-r--r-- 1 zzz staff 317K May 25 18:22 spring-boot-loggerbysize.log
3.4 Demo the rolling log file by date
We can define the date-rolling policy in src/main/res/logback-spring.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- All log files located in logs file of the project -->
<property name="LOGS" value="./logs" />
<!-- Define the console log format -->
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFileByDate"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/spring-boot-loggerbyate.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/spring-boot-loggerbydate-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at error level -->
<root level="error">
<appender-ref ref="RollingFileByDate" />
<appender-ref ref="Console" />
</root>
<!-- LOG "com.test*" at TRACE level -->
<logger name="com.test" level="trace" additivity="false">
<appender-ref ref="RollingFileByDate" />
<appender-ref ref="Console" />
</logger>
</configuration>
Then you can test by running a infinite loop which print a lot of logs. Then you would get these files:
-rw-r--r-- 1 zzz staff 393K May 25 18:34 spring-boot-loggerbyate.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:31 spring-boot-loggerbydate-2019-05-25.0.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:31 spring-boot-loggerbydate-2019-05-25.1.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:31 spring-boot-loggerbydate-2019-05-25.2.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:32 spring-boot-loggerbydate-2019-05-25.3.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:32 spring-boot-loggerbydate-2019-05-25.4.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:32 spring-boot-loggerbydate-2019-05-25.5.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:33 spring-boot-loggerbydate-2019-05-25.6.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:33 spring-boot-loggerbydate-2019-05-25.7.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:33 spring-boot-loggerbydate-2019-05-25.8.log
-rw-r--r-- 1 zzz staff 1.0M May 25 18:34 spring-boot-loggerbydate-2019-05-25.9.log
4. Conclusion
You can get more information about logback appenders by reading logback logging appender configurations.