Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap log4j or create custom logger?

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...
like image 257
Brian Avatar asked Jan 20 '23 17:01

Brian


1 Answers

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.

like image 191
Nathan Ryan Avatar answered Jan 28 '23 04:01

Nathan Ryan