Logback RollingFileAppender

Logback RollingFileAppender appends log events into a file with the capability to rollover (archive the current log file and resume logging in a new file) based on a particular schedule, such as daily, weekly, monthly or based on log file size.

For quick reference, this is configuration file we will be discussing further in the post. It has configuration for console appender and rolling file appender.

<configuration>
	<property name="LOG_ROOT" value="c:/temp/logs" />
	<property name="LOG_FILE_NAME" value="application" />
	
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
		</encoder>
	</appender>

	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_ROOT}/${LOG_FILE_NAME}.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<fileNamePattern>${LOG_ROOT}/${LOG_FILE_NAME}-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
			<!-- each archived file's size will be max 10MB -->
			<maxFileSize>10MB</maxFileSize>    
			<!-- 30 days to keep -->
			<maxHistory>30</maxHistory>
			<!-- total size of all archive files, if total size > 100GB, it will delete old archived file -->
			<totalSizeCap>100GB</totalSizeCap>
		</rollingPolicy>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
		</encoder>
	</appender>
	
	<logger name="com.howtodoinjava.app" level="INFO" additivity="false">
        <appender-ref ref="FILE"/>
    </logger>

	<root level="ERROR">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
	</root>
</configuration>

1. Dependencies

Logback requires three modules in the application runtime i.e. logback-corelogback-classic and slf4j-api.

<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-core</artifactId>
	<version>1.2.10</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.10</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.32</version>
</dependency>

Note that logback-classic will automatically pull in the logback-core and slf4j-api, so adding logback-classic dependency is enough.

2. Rolling and Triggering Policies

2.1. TimeBasedRollingPolicy

To configure rolling logs, we can use TimeBasedRollingPolicy that has the following attributes:

  • fileNamePattern: defines the name of the rolled-over (archived) log files. The rollover period is inferred from the date pattern specified in its value. The default pattern ‘yyyy-MM-dd’.
  • maxHistory (optional): controls the maximum number of archive files to keep, asynchronously deleting older files.
  • totalSizeCap (optional): controls the total size of all archive files. Oldest archives are deleted asynchronously when the total size cap is exceeded.
  • cleanHistoryOnStart: is false, by default, that means archive removal is normally performed during rollover. If set to true, archive removal will be executed on the appender startup.

Prudent mode supports multiple-JVM writing to the same log file.

The given logback configuration creates daily rolled-over logs, keeps 30 days’ worth of history, but at most 3GB of total archived logs. Older logs start getting deleted as soon as the size limit is breached.

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>application.log</file>
    <prudent>true</prudent>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>application.%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>30</maxHistory>
      <totalSizeCap>3GB</totalSizeCap>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

2.2. SizeAndTimeBasedRollingPolicy

Size-based rolling policy allows to rollover based on file on each log file. For example, we can rollover to a new file when the log file reaches 10 MB in size.

The maxFileSize is used to specify the size of each file when it gets rolled over.

Also, notice the token '%i' that is used to create new files with an increasing index, starting at 0. This is needed to create multiple small files with maxFileSize limit in place of a single large rolled-over file.

The given logback configuration creates daily rolled-over logs with max log size of 10 MB, keeping 30 days worth of history, but at most 10 GB of total archived logs. Older logs start getting deleted as soon as the size limit is breached.

<configuration>
  <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>application.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <fileNamePattern>application-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
       <maxFileSize>10MB</maxFileSize>    
       <maxHistory>30</maxHistory>
       <totalSizeCap>10GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>


  <root level="DEBUG">
    <appender-ref ref="ROLLING" />
  </root>
</configuration>

2.3. SizeBasedTriggeringPolicy

We already discussed size based trigger policy in the previous section with the use of maxFileSize. It is that same thing, with a little syntax difference where SizeBasedTriggeringPolicy is declared in a separate section.

<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  <fileNamePattern>application-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
  <maxHistory>60</maxHistory>
  <totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  <maxFileSize>10MB</maxFileSize>
</triggeringPolicy>

3.Conclusion

In this logback rolling file appender tutorial, we learned to configure the time and size-based rolling policies with examples. We also saw the sample logback configuration file with console and rolling file appenders, for reference.

Happy Learning !!

Download Sourcecode

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.