Customizing Rolling Appenders of Log4j

Problem:
Apache's Log4j is a versatile event logging system.I admire the flexibility of Log4j.
Once a situation came where I need to append the datestamp to the filename and also limit the filesize to some customizable value.

The first could be obtained by using DailyRollingFileAppender(which however is blamed as prone to lose some logs--why? see apache docs for log4j) and the second could be obtained by using RollingFileAppender.

My requirements could be met by two ways:
a)if I could control the filenames to be used in RollingFileAppender
b)or rollover the files when a certain size is reached, when I use DailyRollingFileAppender

I thought of trying to extend RollingFileAppender and attempted to introduce a twist in my class.
The main methods for the RollingFileAppender that are required for me to override were subAppend(..) and rollOver(..).

After trying a bit I got the following class working for me: Log4j.properties file: I got it working. The filenames were GeneralLog.log,GeneralLog.log2011-03-28.i where i=1 to 100.
The filesize was limited to 6KB (5KB +1)--a small value was used for testing purpose,can be customized as per requirement.

Writing own working class is quite interesting:))). Happy Coding!

1 comment: