Make Selenium Easy

Log4j2 Tutorial 7 – Working Mechanism of Default Rollover Strategy

Last updated on April 30th, 2021 at 04:21 pm

Introduction

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.

We are going to learn more about Default Rollover Strategy in this post.

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.

Parameters of RollingFileAppender

There are multiple parameters are provided for RollingFileAppender but below are some which are used frequently and required to understand its working mechanism.

Parameter NameTypeDescription
fileNameStringThe name of the file to write to. If the file, or any of its parent directories, do not exist, they will be created.
filePatternStringThe pattern of the file name of the archived log file. The format of the pattern is dependent on the RolloverPolicy that is used.

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.

Parameters of Default Rollover Strategy

Parameter NameTypeDescription
fileIndexStringIf set to “max” (the default), files with a higher index will be newer than files with a smaller index. If set to “min”, file renaming and the counter will follow the Fixed Window strategy described below.
minintegerThe minimum value of the counter. The default value is 1.
maxintegerThe maximum value of the counter. Once this values is reached older archives will be deleted on subsequent rollovers. The default value is 7.

The default rollover strategy supports three variations for incrementing the counter. To illustrate how it works, suppose that the min attribute is set to 1, the max attribute is set to 3, the file name is “foo.log”, and the file name pattern is “foo-%i.log”.

Number of rolloversActive output targetArchived log filesDescription
0foo.logAll logging is going to the initial file.
1foo.logfoo-1.logDuring the first rollover foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.
2foo.logfoo-2.log, foo-1.logDuring the second rollover foo.log is renamed to foo-2.log. A new foo.log file is created and starts being written to.
3foo.logfoo-3.log, foo-2.log, foo-1.logDuring the third rollover foo.log is renamed to foo-3.log. A new foo.log file is created and starts being written to.
4foo.logfoo-3.log, foo-2.log, foo-1.logIn the fourth and subsequent rollovers, foo-1.log is deleted, foo-2.log is renamed to foo-1.log, foo-3.log is renamed to foo-2.log and foo.log is renamed to foo-3.log. A new foo.log file is created and starts being written to.

By way of contrast, when the fileIndex attribute is set to “min” but all the other settings are the same the “fixed window” strategy will be performed.

Number of rolloversActive output targetArchived log filesDescription
0foo.logAll logging is going to the initial file.
1foo.logfoo-1.logDuring the first rollover foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.
2foo.logfoo-1.log, foo-2.logDuring the second rollover foo-1.log is renamed to foo-2.log and foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.
3foo.logfoo-1.log, foo-2.log, foo-3.logDuring the third rollover foo-2.log is renamed to foo-3.log, foo-1.log is renamed to foo-2.log and foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.
4foo.logfoo-1.log, foo-2.log, foo-3.logIn the fourth and subsequent rollovers, foo-3.log is deleted, foo-2.log is renamed to foo-3.log, foo-1.log is renamed to foo-2.log and foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.

All the above points I have extracted from official Log4j2 documentation.

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.
#ThanksForReading
#HappyLearning

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 7 – Working Mechanism of Default Rollover Strategy

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