Make Selenium Easy

Log4j2 Tutorial 6 – Introduction to RollingFileAppender and its Triggers and Strategies

Last updated on May 1st, 2021 at 12:15 am


We have already learned the basics but important concepts of Log4j2 in series of tutorials in Log4j2 Tutorials. Now we know to create an XML configuration file to print logs into a File and onto the console.

We are going to learn about Rolling File Appender in this post. You must be thinking about how it is different from writing into a file that we have already learned.

We have already configured an XML document with a File appender to write logs into a file and configure if we want to append logs in the same file or want to create a new one. Rolling File Appender will be a little more configurable in which we can configure when to create a new log file and how to create a new log file.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.


The RollingFileAppender is an OutputStreamAppender that writes to the File named in the fileName parameter and rolls the file over according to the TriggeringPolicy and the RolloverPolicy. A RollingFileAppender requires a TriggeringPolicy and a RolloverStrategy. The triggering policy determines when a rollover should be performed while the RolloverStrategy defines how the rollover should be done. Ref – Log4j – Documentation

Triggering Policies

As we understand the basic idea behind Rolling file appender let’s understand the trigger policies provided with Rolling file appender

OnStartup Triggering Policy

The OnStartupTriggeringPolicy policy causes a rollover if the log file is older than the current JVM’s start time and the minimum file size is met or exceeded. It takes a parameter called minSize which is the minimum size the file must have to roll over. A size of zero will cause a rollover no matter what the file size is. The default value is 1, which will prevent rolling over an empty file.

TimeBased Triggering Policy

The TimeBasedTriggeringPolicy causes a rollover once the date/time pattern no longer applies to the active file. This policy accepts an interval attribute that indicates how frequently the rollover should occur based on the time pattern and a modulate boolean attribute. This might sound confusing but do not worry we will see examples to understand.

SizeBased Triggering Policy

As the name implies this trigger policy is based on the size of the log file. It causes a rollover once the file has reached the specified size. The size can be specified in bytes, with the suffix KB, MB or GB. When combined with a time based triggering policy the file pattern must contain a %i otherwise the target file will be overwritten on every rollover as the SizeBased Triggering Policy will not cause the timestamp value in the file name to change. When used without a time based triggering policy the SizeBased Triggering Policy will cause the timestamp value to change.

Cron Triggering Policy

We can schedule the rollover using cron syntax as we do to schedule a Jenkins job. This policy is controlled by a timer and is asynchronous to processing log events, so it is possible that log events from the previous or next time period may appear at the beginning or end of the log file. The filePattern attribute of the Appender should contain a timestamp otherwise the target file will be overwritten on each rollover.

We can combine multiple triggering policies and that can be called a Composite Triggering Policy.

Rollover Strategies

Providing Rollover strategies is optional and if no RolloverStrategy is configured, RollingFileAppender will use the DefaultRolloverStrategy.

Default Rollover Strategy

The default rollover strategy accepts both a date/time pattern and an integer from the filePattern attribute specified on the RollingFileAppender itself. If the date/time pattern is present it will be replaced with the current date and time values.

If the pattern contains an integer it will be incremented on each rollover. If the pattern contains both a date/time and integer in the pattern the integer will be incremented until the result of the date/time pattern changes.

If the file pattern ends with “.gz”, “.zip”, “.bz2”, “.deflate”, “.pack200”, or “.xz” the resulting archive will be compressed using the compression scheme that matches the suffix.

The default rollover strategy supports three variations for incrementing the counter. We will see all of them in detail.

DirectWrite Rollover Strategy

The DirectWriteRolloverStrategy causes log events to be written directly to files represented by the file pattern. With this strategy, file renames are not performed.

If the size-based triggering policy causes multiple files to be written during the specified time period they will be numbered starting at one and continually incremented until a time-based rollover occurs.

GitHub Link to clone project

Log4j2 Example Codes

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Log4j2 Tutorial 6 – Introduction to RollingFileAppender and its Triggers and Strategies

by Amod Mahajan time to read: 4 min
%d bloggers like this: