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:
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:
You can test this by running an infinite loop that prints a lot of logs. You will get files like these:
Demo rolling log file by date
You can define the date-rolling policy in src/main/resources/logback-spring.xml as follows:
You can test this by running an infinite loop that prints a lot of logs. You will get files like these:
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: