Sunday 26 April 2009

Log4J Log Rolls At Midnight

I found a requirement for the Log4J DailyRollingFileAppender to roll logs at midnight. This doesn't seem to be possible in Log4J 1.2 unless the application itself issues a logging event at midnight. If the application happens to be idle at that time, a log roll isn't going to happen. I guess you could do make the application do this by running a Timer, for example, thereby ensuring that a logging event is issued and forcing the appender to roll the logs.

It seems to me that it would be convenient if the appender could take care of rolling files on a schedule itself, so I've added this behaviour to the Log4J TimeAndSizeRollingAppender hosted on my site at www.simonsite.org.uk.

The new stuff doesn't try to be too clever. It simply looks at the DatePattern config parameter of the appender (this is in common with the DailyRollingFileAppender) and assumes that the end-point of a logging period implied by it is when rolling should happen. So if you have a DatePattern set to 'yyyy-MM-dd' the appender assumes that it should force a file roll at the end of every day.

4 comments:

Anonymous said...

Thanks for the nice features. How can I turn off backup feature (I mean the ".index" at the end of files). I don't want to enforce any size limit, so I just need to roll on a certain time (mid-night)? How can I fix the configuration to not break files into smaller pieces. I know I can always set the max size to a big value but it keeps adding the index at the end of the log file, which breaks my current logging file patterns.

Thank you.
Neo

Greg-- said...

Hi,
you've done a great job with the "alternate" DailyRollingFileAppender. Lets hope that it will be integrated with a future version of log4j sometime.

Anonymous said...

Nice job exactly what is was looking for!

Simon said...

Hi Neo,

I'm afraid that a central feature of the appender is to add an index to the end of the backup filename whenever a rollover occurs. I don't have immediate plans to change it.