RollingFile Appender in log4j2.xml
- February 19, 2020
There are several Appenders in log4j2 that you can use to send application logs to a specified location. Use File Appender to pick the application logs and store it in a file specified at some location.
You need to follow certain steps to customize log4j2.xml to implement File Appender. It starts with creating a simple Mule application. The following is example application:
The scheduler configuration will be like the following:
The first logger component before a transform message component will look like the following:
The transform message component will be configured as shown below:
The first logger component after a transform message is configured as shown below:
The second logger after a transform message is configured as shown below:
How to configure log4j2.xml file
When you create any MuleSoft project, the default location of log4j2.xml is under the src/main/resources directory.
The configuration of log4j2.xml will be:
In the above screenshot of log4j2.xml, we’ve defined two properties:
- fileSize: the size of the log file
- numberOfFiles: the maximum number of log files that can be created at specified location
The RollingFile Appender can be defined using three properties:
- name: unique name assigned to RollingFile Appender
- fileName: name of the log file to be created
- filePattern: name pattern of the file to be created
%i is defined to assign a number to a log file. For example, mule-testapp-1, mule-testapp-2…….
You can define a trigger based on file size using SizeBasedTriggeringPolicy. If there’s a limit set using this policy, then a new file will get created after reaching this limit.
DefaultRolloverStrategy is used to set a limit on the number of files and overwrite the same number of files again and again. It means that initially, only 10 files will be created in the specified location. Those 10 files will get overwritten again. In the above example, we have two RollingFile Appenders, one for the INFO level and another for the ERROR level.
Both appenders will write the files in different locations.
Once you complete all the changes and start running the application, log files will be written at specified locations.
Initially the first 10 log files will be written.
Those files will get overwritten again.
— By Manish Prabhu