I understand that i can use a DailyRollingFileAppender to roll the log file every month, day, half-day, hour or minute. But how can i configure log4j to roll the log file every 15 minutes.
If this is not possible by configuration, please suggest/direct me on how to extend log4j's file appender to achieve this.
Thanks and Regards.
I know its late to reply , but below solution will help you out and other user who search the same question and stuck back to this thread.
I modified DailyRollingFileAppender.java to include time interval clause in file rolling , this will allow user to set minutes interval at which rolling has to be done
include file from here http://abheygupta.com/DailyRollingFileAppender.java
and configure this way ,tim interval can have any values from 60 factors [1,2,3,4,5,6,10,12,15,20,30,60] log4j.appender.mtlog_api11.DatePattern='.'mmHHMMddyyyy log4j.appender.mtlog_api11.TimeInterval=10
it will work normal in other DatePattern than specified above .
Here is code I use for Hourly. You can alter it for every 15 minutes - see method
nextCalendar()
. This code is based on DatedFileAppender.Here is my appender XML snippet:
I created this appender. It is copy of DailyRollingFileAppender just with few changes. First I remove type HALF_DAY (here it doesnt make any sence) and add one variable timeInterval. Now this log to the file every x something depends on datePatter. For examle:
this log every 2 minutes
this log every 3 hours
CustomRollingFileAppender :
The Javadoc for DailyRollingFileAppender in Log4J indicates that the time-based rolling only occurs on unit-based rollovers (day, week, month, etc.). That would mean the closest you could get with that pattern is
'.'yyyy-MM-dd-HH-mm
, which would roll over every minute.My recommendations would be to do one of the following:
FileAppender
that uses logic borrowed fromDailyRollingFileAppender
to make the computationRollingPolicy
that uses logic borrowed from the LOGBack time calculations (which will be very similar to the ones in Log4J)By the way, if you choose the latter, I'd recommend that you consider coding to the SLF4J API, and use LOGBack (or Log4J) as the underlying implementation.
With logback, you can subclass the RollingFileAppender.
Then use that appender as your appender with a rolling policy
More details here