Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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

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

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.

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.

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.

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.

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

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.

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.

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

Uncategorized

Post navigation

Previous Post: How To Push Code From Eclipse To GitHub Repo Using Access Token?
Next Post: Git Tutorial 30 – Understand Soft, Mixed And Hard Options Of Git Reset

Related Posts

git reset Uncategorized
June 8, 2018 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Generate random numbers using Javafaker API – Java Uncategorized
July 25, 2017 – Make Selenium Easy Uncategorized
Git Tutorial 8 – Git Push – Upload Committed Local Repository Changes To Remote Repository Branch Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark