Make Selenium Easy

Log4j2 Tutorial 8 – XML configuration for Default Rollover Strategy with SizeBasedTriggeringPolicy

Introduction

This post belongs to End to End Log4j2 Tutorials.

We have already learned many concepts of Log4j2 and as of now, we are covering the Rolling File Appender concept and Working Mechanism Of Default Rollover Strategy. We are going to learn these concepts with examples 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.

SizeBasedTriggeringPolicy, 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.

Sample XML configuration

Similar to Console and File appender, we need to use RollingFile appender. Properties such as fileName (The name of the file to write to) and filePattern (The pattern of the file name of the archived log file) which we have learned in the previous post. We have defined a policy SizeBasedTriggeringPolicy with a property size for a value of 1KB. It means that as soon as the size of the current log file reaches 1KB, log file will be rolled as per filePattern.

Remember we need to keep log4j2.xml file in the resource folder. I have kept it inside src/main/resource folder.

Simple Java program

We will use the same Java program used in previous examples-

Let’s run the above program the first time and refresh the project. You will see changes as below. You can also check the size of file.

An initial log file named “application.log” is created which we have mentioned in fileName property in RollingFile. Rerun the same program multiple times at least 3 more times. Why I want you to run at least 3 times because after these runs size of the log file will be exceeding 1 KB. Once it exceeds, the current log file will be renamed based on filePattern and a new empty log file i.e. application.log will be created.

Run the same program multiple times continuously at least till a minute and you will see multiple log files as below:-

You should notice an integer value in the archived log files name. The same I have covered in the previous post.

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.

Remeber that logs for an execution may be divided in multiple log files based on size. Log file will be rolled over as soon it reaches the size.

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 8 – XML configuration for Default Rollover Strategy with SizeBasedTriggeringPolicy

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