1
votes

I am writing a custom wrapper over log4j2. Using https://logging.apache.org/log4j/2.x/manual/customloglevels.html#CustomLoggers

The requirement was to choose the appender based on the object type being logged. The approach I am thinking was to extend the log4j2 logger and override log methods(like info), In those methods, check the object type and call the required appender.

Now I want my application to use slf4j facade instead of log4j2 directly. Need help on how to bind my custom log4j2 wrapper to slf4j, with less effort :)

One option I am thinking(haven't tried yet) is to refer my customlogger wrapper in log4j-slf4j-impl and generate jar and use the same in application, https://github.com/apache/logging-log4j2/blob/f838bd6f42ea104e692e9e2227dcc54283a41901/log4j-slf4j18-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java

  • Do we have any better approach to solve this problem?
1
This might be helpful, I've built some applications using Ninja Framework, and I know they use sl4j as their logging solution. You might take a look at their logging source for ideas on how they've implemented it. ninjaframework.org/documentation/logging.htmlNicholas Hirras

1 Answers

0
votes

If you want to choose an Appender based on an object type one way to do it is to use Log4j 2's RoutingAppender. The RoutingAppender may be configured with either a pattern or a Script. The pattern is used to extract something from the log event. That value is then matched against the configured options from which a "route" to an Appender is chosen. When using a Script the script returns the name of the "route" to use.