Log4J - Logging in Files
Log4j provides org.apache.log4j.FileAppender class to write your logging information into a file.
FileAppender has the following configurable parameters:
Let's see one example for FileAppender:
Hello this is a debug message Hello this is an info message
If you wish to have an XML configuration file equivalent to the above log4j.properties file, then below is the content:
Logging in Multiple Files
We can also write multiple messages into multiple files for certain reasons, for example, if the file size reached a certain threshold.
To write your information about logging into multiple files, you have to use org.apache.log4j.RollingFileAppender class which extends the FileAppender class and inherits all its properties.
There is only one important parameter in addition to the ones mentioned above for FileAppender:
DatePattern: This indicates when to rollover the file and the naming convention to be followed. By default, rollover is performed at midnight per day.
DatePattern is used to control the rollover schedule using one of the following patterns:
Let's see a sample configuration file log4j.properties to generate log files rolling over at midday and midnight of each day: