tags:

views:

140

answers:

2

I have a web application that dumps logging information in a file using log4j RollingFileAppender. The application is currently configured to dump only WARN and higher logging information so that the disk I/O do not impact performance on the server.

However, I would like to know the last X debug information when an error occurs. Is there a way to configure log4j to dump the last 25 lines of debug information, as well as the error, in a file?

I presume this can be done using a custom appender. I tried searching for an example and could not find one.

A: 

I think writing your own is the way to go. Can you implement Appender and delegate each call to it to an underlying RollingFileAppender ?

In your implementation simply store the most recent 'n' log messages, and write through to the rolling file appender. If you encounter an error message, then dump your stored debug messages to the rolling file appender.

Brian Agnew
A: 

Yes, the only way I am aware of to go about this is to write your own Appender. Either extend RollingFileAppender or write an Appender that delegates to a RollingFileAppender. In your new Appender, keep a history of the last N logging events and when something is logged at ERROR level, dump your history of logging events. I started to implement something like this myself but never completed it.

Eddie