Author: Manish Prabhu
There are several appenders in log4j2 which can be used to send application logs to a specified location. File appender is used to pick the application logs and store it in a file specified at some location. We need to follow certain steps to customize log4j2.xml in order to implement file appender.
Step 1: Create a simple mule application. The example application I have created is:
Scheduler configuration will be like:
The first logger component before a transform message component will look like:
The transform message component will be configured as:
The first logger component after a transform message is configured as:
The second logger after a transform message is configured as:
How to configure log4j2.xml file:
When you create any mulesoft project, the default location of log4j2.xml is under src/main/resources directory. The configuration of log4j2.xml will be:
In the above screenshot of log4j2.xml, I have defined two properties.
- fileSize -size of log file
- numberOfFiles- 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 e.g.mule-testapp-1, mule-testapp-2…….
You can define a trigger based on file size using SizeBasedTriggeringPolicy. If there is 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 & overwrite the same number of files again and again. It means that initially only 10 files will be created in specified location. Those 10 files will get overwritten again. In the above example, we have two RollingFile appender one for INFO level and another for 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.