Sunday, 23 September 2007

Log4J Rolling Appender

I've coded up a file-writing Log4J Appender, tried-and-tested with Log4J 1.2.14 (though it should work with earlier Log4J 1.2.x releases). The single Appender class bundles 3 features together:
  • Rolls log files at configurable time intervals
  • Rolls log files at configurable sizes
  • Removes older log files once a configurable limit has been reached
I've posted the source code, compiled classes, and the Javadoc at www.simonsite.org.uk.

Please post feedback here. Thanks!

11 comments:

Tim said...

Nice. I wrote an appender like this for my former employer. It only included the scavenging aspects, although it had options to either remove the older files or just gzip them (and ultimately remove them after another period of time).

I'll certainly be using this in my new environment, thanks for the contribution.

David Iglesias Garcia said...

Hello.
your work is excelent.
Where I download the javaDoc and SourceCode.
Thanks David.

Maxim Veksler said...

Hello Simon,

I would like to that you very much for your work. You have saved us a great deal of development aiming at the same target exactly. We've been using your code for about a year now and so far it works great without a single problem.

p.s.
Why won't you submit your patches to the log4j project? Wouldn't it be better for this to be included in the official log4j so that everyone could benefit.

Again,
Thank you.

Maxim.

Anonymous said...

Excellent. Saved my time in writing a custom appender. Ready to be used for a telecom major

Anonymous said...

Well said.

Anonymous said...

Hi Simon,

I am getting the following error:
log4j:ERROR Log file scavenger failed
java.lang.NullPointerException
at org.apache.log4j.appender.LogFileScavenger.logFileList(LogFileScavenger.java:109)
at org.apache.log4j.appender.LogFileScavenger.scavenge(LogFileScavenger.java:121)
at org.apache.log4j.appender.LogFileScavenger.run(LogFileScavenger.java:64)
at java.lang.Thread.run(Thread.java:619)


Any idea what could be the problem?

- Nick

Simon said...

Nick - thanks for spotting that. NPE reproduced and fixed (see later posts).

Unknown said...

hi Simon,
Looks like there are couple of issue, when i tested it with yyyy-MM-dd-HH-mm and a scavenge interval of 300 and rollover size of 4KB, i see the file rolling over after 4KB but the zip file is empty any clues ??
-Jayant

Simon said...

Hi jayt,

Compression is carried out in a thread, so timing effects are expected. It is normal to see that an apparently "empty" zip file has been created whilst compression is still in progress.

Also note that if the system is not shutdown cleanly, i.e. without proper cleanup of Log4J, you can expect compression operations to be abandoned. This might mean that bad ZIP/GZIP files appear in the log list. However, you will not lose the backup file in this case.

Hope this helps - let me know if you still see any issues.

Thanks,

Simon

Unknown said...

Hey Simon,

Its an instant code.saved a lots of time.Was new to Log4j ,ur code was pretty helpful.

Thanks ,
Sindhu

Frank Taylor said...

I created a DailyRollingFileAppender that also creates directories. see: http://dl.getdropbox.com/u/688127/public-web-site/DailyRollingFileAppenderToDirectories.htm