Alex Rivera | Logout

LOG4J: Modify logged message using custom appender

Asked 2011-10-13T15:11:56.817
19

For security reasons, I need to look at every logged message in my app and possibly modify it before it goes to the log file. I figured I could write a custom appender (extending DailyRollingFileAppender) and override subAppend(LoggingEvent event). Problem is, there's no setter for the message text in LoggingEvent, and the message is a private attribute. I could create a new LoggingEvent with my modified message, but the API doesn't make it easy to copy over the rest of the original LoggingEvent. It all seems designed to discourage meddling with the message in a custom appender.

The only other option I can see is to modify hundreds of logging statements to call a new global method that can modify the text first and then make the Log4J call. I'd rather not!

Has anyone else had the need to modify the logged message in a custom appender?

Edit
Report

1 Answer

4

Another option is to customize the Layout used by your appender. Since layout is responsible for serializing log event to a string, I'd check if twitching the layout is less complicated than changing the appender and events going through. Just an idea...

answered 2011-11-18T12:11:45.663

Your Answer