Wrap log4j or create custom logger?

Brian picture Brian · May 17, 2011 · Viewed 9k times · Source

I have an application that needs to log two different types of messages: application log messages and audit messages. Application log messages match the standard lo4j Logger perfectly, but there are several required parameters for the audit log.

I think I need to wrap log4j to add the additional required parameters to the debug(), info(), etc methods, but I hate the idea of wrapping log4j. Should I:

  1. Wrap log4j completely and provide my own Logger class that calls a log4j logger behind the scenese?
  2. Extend the log4j Logger class and add "audit log" methods with my required parameters?
  3. Do something even more elegant so I'm not wrapping a logging library...

Answer

Nathan Ryan picture Nathan Ryan · May 17, 2011

The "message" of the error, warn, etc. methods of a log4j Logger is an arbitrary object; it doesn't need to be a string. You can create your own "message" class to contain the different parameters. Loggers can append the data differently by using a custom Layout class for the appender of the audit logger.