Rolling log files by size or by date in springboot apps
The purpose of this post
This post demonstrates how to roll log files by date or by size in Spring Boot applications.
Environments
- Spring Boot 1.x and 2.x
Rolling log files in Spring Boot apps
What’s the default logging configuration in Spring?
By default, Spring Boot uses Logback as the default logger. However, there are some limitations if you only use application.properties
to configure 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 configurationlogging.file=springboot-logger.log # this is the logging target file
You can configure the root logging level and package logging level, but what if you want to roll log files by date or by size?
Add Logback configuration file to your project
When a file in the Spring Boot 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
The logback-spring.xml
file is recommended.
Demo rolling log file by size
You can define the rolling policy in src/main/resources/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="RollingFileBySize" /> <appender-ref ref="Console" /> </root>
<!-- LOG "com.test*" at TRACE level --> <logger name="com.test" level="trace" additivity="false"> <appender-ref ref="RollingFileBySize" /> <appender-ref ref="Console" /> </logger>
</configuration>
You can test this by running an infinite loop that prints a lot of logs. You will get files like these:
-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
Demo rolling log file by date
You can define the date-rolling policy in src/main/resources/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-loggerbydate.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>
You can test this by running an infinite loop that prints a lot of logs. You will get files like these:
-rw-r--r-- 1 zzz staff 393K May 25 18:34 spring-boot-loggerbydate.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
Summary
In this post, we explored how to configure log file rotation by size or by date in Spring Boot applications using Logback. We covered the default logging configuration, how to add a Logback configuration file, and provided examples of rolling log files by size and by date. By following these steps, you can effectively manage log files in your Spring Boot applications.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!