Log4j2 Tutorial 7 – Working Mechanism of Default Rollover Strategy

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.

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

Parameter Name Type Description
fileName String The name of the file to write to. If the file, or any of its parent directories, do not exist, they will be created.
filePattern String The pattern of the file name of the archived log file. The format of the pattern is dependent on the RolloverPolicy that is used.

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.

Parameter Name Type Description
fileIndex String If 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.
min integer The minimum value of the counter. The default value is 1.
max integer The 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 rollovers Active output target Archived log files Description
0 foo.log All logging is going to the initial file.
1 foo.log foo-1.log During the first rollover foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.
2 foo.log foo-2.log, foo-1.log During the second rollover foo.log is renamed to foo-2.log. A new foo.log file is created and starts being written to.
3 foo.log foo-3.log, foo-2.log, foo-1.log During the third rollover foo.log is renamed to foo-3.log. A new foo.log file is created and starts being written to.
4 foo.log foo-3.log, foo-2.log, foo-1.log In 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 rollovers Active output target Archived log files Description
0 foo.log All logging is going to the initial file.
1 foo.log foo-1.log During the first rollover foo.log is renamed to foo-1.log. A new foo.log file is created and starts being written to.
2 foo.log foo-1.log, foo-2.log During 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.
3 foo.log foo-1.log, foo-2.log, foo-3.log During 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.
4 foo.log foo-1.log, foo-2.log, foo-3.log In 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.

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