1. Overview
In this article, we will focus on the LogBack RollingFileAppender and few examples.
The Logback library, the successor of Log4j library is a logging framework that is modern, fast, and flexible. This library supports uploading the log statements to a file and archives the file when it meets certain time and size conditions.
For large and most frequently used applications, the log statements pile up and increase your file size. When you try to download the file from your server for debugging purposes, you struggle to view the contents of the huge file directly thus requiring you to split the file into multiple small files manually.
So, to avoid this issue, you can archive your file regularly based on certain conditions like time or size. The RollingFileAppender does that.
2. LogBack RollingFileAppender example
The RollingFileAppender is a subclass of FileAppender and allows you to roll the file, meaning archive file once it meets a certain condition.
2.1. Rolling File Appender dependencies
First let’s add the LogBack dependencies in your project to use the Rolling File Appender. We are using version 1.2.6 here. However, you can check the Log Back release page and use the latest version.
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.6</version> </dependency>

The logback-classic jar transitively pulls in all other required jars logback-core and slf4j-api into the project.
If you don’t explicitly specify the configuration file, then the LogBack uses a default configuration and displays the logs in the console at DEBUG level.
However, you can customize this behavior by defining the configuration files and integrate the Rolling File Appender.
The Rolling File Appender has two important sub-components: Rolling Policy
and Triggering Policy
. The rolling policy handles the roll over whereas triggering policy determines the condition or trigger point to start the archive of the log file.
3. LogBack RollingFileAppender policies
The RollingPolicy handles the rollover process, meaning takes care of moving the file and renaming it. Let’s discuss the following rollover strategies:
- Date and time
- Size
- Both time and size
- Automatic compression
3.1. Roll over based on date and time using TimeBasedRollingPolicy
The popular TimeBasedRollingPolicy rolls over the log file based on time (Example: by day/ minute/ hour / month). It handles both the rollover and trigger. Time is the trigger point to archive the log file.
For example, the below code uses the TimeBasedRollingPolicy. The fileNamePattern defines the format of the archive file. The pattern yyyy-MM-dd-HH-mm instructs the LogBack to roll over the log file every minute.
<configuration debug="true"> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}spring%d{yyyy-MM-dd-HH-mm}.log</fileNamePattern> <maxHistory>7</maxHistory> <totalSizeCap>10MB</totalSizeCap> </rollingPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration>
logging.file.path=D:\\log\\

The maxHistory controls the maximum number of archive files to keep, whereas totalSize controls the total size of your archived files. Whenever the archive file reaches this specified limit, the LogBack deletes the older archive files.
The below log shows that the LogBack deleted the archive file after reaching the totalSize limit of 1MB.
19:24:05,688 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [D:\log\spring.log] to [D:\log\spring2021-09-27-19-23.log] 19:24:05,690 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - first clean up after appender initialization 19:24:05,690 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - Multiple periods, i.e. 336 periods, seem to have elapsed. This is expected at application start. 19:24:05,701 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - Deleting [D:\log\spring2021-09-27-19-23.log] of size 1 MB 19:24:05,702 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - Removed 1 MB of files
3.2. Roll over based on size using FixedWindowRollingPolicy & SizeBasedTriggeringPolicy
It triggers based on the size of the log file. If the log file size grows larger than the specified size, it will ask the RollingFileAppender
to trigger the rollover of the current log file.
SizeBasedTriggeringPolicy
is only a triggering policy and doesn’t handle the log file rollover such as archiving the file and renaming it. So you must use this policy along with other rolling policies.
It accepts only one parameter, namely maxFileSize, with a default value of 10 MB.
The below configuration instructs the RollingFileAppender to archive the log file once it reaches 50KB.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${LOG_PATH}spring.%i.log</fileNamePattern> <minIndex>1</minIndex> <maxIndex>3</maxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>50KB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration>
Here, we used the FixedWindowRollingPolicy to handle the roll-over process of the files.
The maxIndex of the fixed window is 3. Thus, it limits the maximum number of archive files by count 3 such as spring.1.log, spring.2.log, and spring.3.log files. After it reaches 3 archive files, it keeps on replacing the existing archive files with new data instead of creating a new one.

3.3. Roll over based on time and size using the SizeAndTimeBasedRollingPolicy
You may wish to archive your file based on both size and also time. This rolling policy will help you achieve it.
For example, the below code uses the SizeAndTimeBasedRollingPolicy. It triggers rollover every minute.
Whenever the current log file reaches the specified maxFileSize before a minute, then it archives with an increasing index, starting at 0 like output-2021-09-27-22-22.0.txt, so on. Each file has a maximum file size limit of 50KB.
<configuration> <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${LOG_PATH}output-%d{yyyy-MM-dd-HH-mm}.%i.txt</fileNamePattern> <maxFileSize>50KB</maxFileSize> <maxHistory>60</maxHistory> <totalSizeCap>20GB</totalSizeCap> </rollingPolicy> <encoder> <pattern>%msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="ROLLING" /> </root> </configuration>

3.4. LogBack RollingFileAppender automatic file compression
You may wish to compress your archive files in order to reduce the amount of storage required by them. The LogBack library automatically does it for you. You only need to specify the fileNamePattern value with .gz or .zip suffix.
For example, let’s take the same SizeAndTimeBasedRollingPolicy example and change the fileNamePattern
so that it ends with .gz text. Therefore, the LogBack automatically compresses the archive file to GZIP.
<configuration> <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${LOG_PATH}output-%d{yyyy-MM-dd-HH-mm}.%i.log.gz</fileNamePattern> <maxFileSize>50KB</maxFileSize> <maxHistory>60</maxHistory> <totalSizeCap>20GB</totalSizeCap> </rollingPolicy> <encoder> <pattern>%msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="ROLLING" /> </root> </configuration>

4. Conclusion
To sum up, we have discussed the RollingFileAppender with various use cases.
Pingback: LogBack multiple log files - TedBlob