Tuesday 18 May 2010

Roll Log4J log files on application startup

A fairly popular feature request for the TimeAndSizeRollingAppender has been to provide the capability to roll a log file when the application starts up. This can be useful if an application is restarted during a period which falls within a logging interval, and when the most recently written log file is smaller than would be needed to trigger a rollover. In response to requests, I've added this feature into the latest release, available from www.simonsite.org.uk. By default, the appender will continue to behave as it has always done. To turn on the rollover on startup feature, somewhat predictably the appender should be configured with the property RollOnStartup set to true.

3 comments:

Adrian Carcu said...

The compression is extremely slow because you're not using buffering in the addEntry method. Try something like this:

byte buf[] = new byte[65536];
for (int len; (len = InputStream.read(buf)) > 0;) {
outputStream.write(buf, 0, len);
}

I've added some timing for this specific section of code and it runs in 200ms vs 34s for 5MB of dummy logging (maybe in real-life conditions the improvement ratio will be slightly less).

Simon said...

Adrian, thanks for pointing that out. By coincidence another user, Oleksiy, has pointed out some performance issues with the GZ compression also. He's contributed some code to fix that and I'm working on testing at the moment. Whilst I'm doing this work I'll be sure to fix up the ZIP compression also. I hope to get a new release out pretty soon, within the next couple of weeks.

Simon said...

ZIP and GZ compression show dramatic performance improvements in the new implementation available on my homepage.