Through this tutorial, I will guide you how to use Logback in a Spring Boot project – both via Spring Boot configuration file and via Logback XML configuration file. You will also learn how to configure rolling files to write logs to files which will be rolled over daily or when a size threshold reaches.

 

1. Why Logback?

Logback is a successor to Log4j – a very popular Java logging framework. Logback brings great improvements over Log4j such as faster performance and smaller memory footprint. Logback also implements SLF4J natively, allowing programmers to migrate from other logging frameworks with ease.

Logback is the default logging framework used by Spring Boot, as illustrated in the following picture:

spring boot log architecture

For more information about Logback, visit Logback homepage.

 

2. How to use Logback via Spring Boot configuration file

Spring Boot allows programmers to easily configure common logging options via the application.properties file. For details about configuring log levels, log patterns and basic file logging, kindly refer to this article: Spring Boot Logging Basics.

In this article, I focus on sharing how to configure rolling file logging with Logback in Spring Boot.



 

3. Logback Rolling File Logging via Spring Boot configuration file

Basically, you just need to specify the following two lines in the application.properties file to enable daily rolling files logging:

logging.file.name=MyApp.log
logging.pattern.rolling-file-name=MyApp-%d{yyyy-MM-dd}.%i.log
Here, the first property specifies the original log file name, and the second one specifies the pattern for the files which will be rolled out the next days (daily rolling). In the pattern, I append date format %d{yyyy-MM-dd} and the ordinal number of the log file (%i) to the file name (MyApp-). Then Logback will store logs for each day in each file like this:

MyApp.log
MyApp-2020-07-29.0.log
MyApp-2020-07-28.0.log
…
Note that with daily rolling file logging, the original log file always contains the latest log messages.

 

Max Log File Size:

You can also specify a threshold for log file size using the following property:

logging.file.max-size=1MB
This keeps the maximum size of each log file around 1MB. A new log file will be rolled out if size of the current log file exceeds this number. If this property is used, Logback will roll out new log files when either the size threshold reaches or a new day has begun.

If this property is not specified, the default size is 10MB.

 

Total Size of Log Files:

You can control the total size of all log files under a specified number, by using the following property:

logging.file.total-size-cap=10MB
If this property is used, Logback will delete the oldest log files in order to keep the total size doesn’t exceed the specified value.

 

Log Files Max History:

You can also specify the maximum number of days that the archive log files are kept, by using the following property:

logging.file.max-history=30
This tells Logback to delete the archived log files that are older than 30 days. If this property is not specified, the default is 7 days.

 

Clean History on Start:

You can tell Logback to clean the archived log files on startup, by setting this property:

logging.file.clean-history-on-start=true
So when our Spring Boot application starts, Logback will clean the old log files based on the values of total size and max history. This property is set to false by default.

Putting all of the above properties together, we end up have the following configuration for Logback in the application.properties file:

logging.file.name=MyApp.log
logging.pattern.rolling-file-name=MyApp-%d{yyyy-MM-dd}.%i.log
logging.file.max-size=1MB
logging.file.total-size-cap=10MB
logging.file.max-history=30
logging.file.clean-history-on-start=true
 

4. Logback Rolling File Logging via XML Configuration file

To configure Logback for a Spring Boot project via XML, create the logback.xml or logback-spring.xml file under the src/main/resources folder. The configuration in XML file will override the logging properties in the application.properties file.

Below is content of the logback.xml file with the rolling file configuration same as in the properties file above:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d [%thread] %-5level %-50logger{40} - %msg%n</pattern>
		</encoder>
	</appender>
	
	<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>MyApp.log</file>
		<encoder>
			<pattern>%d [%thread] %-5level %-50logger{40} - %msg%n</pattern>
		</encoder>
		
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<fileNamePattern>MyApp-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
			<maxFileSize>1MB</maxFileSize>
			<maxHistory>30</maxHistory>
			<totalSizeCap>10MB</totalSizeCap>
			<cleanHistoryOnStart>true</cleanHistoryOnStart>
		</rollingPolicy>
	</appender>
	
	<root level="INFO">
		<appender-ref ref="Console" />
		<appender-ref ref="RollingFile" />
	</root>
</configuration>
Because the Logback XML file overrides the configuration in the properties file, we need to specify two appenders: one for the console and one for the rolling file logging.

 

Conclusion

So far I have shared with you how to use Logback in a Spring Boot project – for rolling file logging. And as you have seen, Spring Boot makes it easy to configure logging via the application properties file, and you can also use a separate Logback XML configuration file if needed.

Watch the following video to see the code in action:

 

Other Spring Boot Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#4Rohan2023-01-31 00:46
How to delete files older than 10 days using application.propertied
Quote
#3Fraaz2022-11-02 06:45
I want learn more about new technologies java update uses
Quote
#2Adrian Ciobanu2021-06-23 12:29
I spent two days looking for something like this and nothing worked. Yours does, and I thank you, Nam.
Quote
#1Anil2021-04-08 11:03
total-size-cap is not working in my case. I could see many rollover files though I set total-size-cap to 10kb (for testing)
Quote