Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reroute log4j to slf4j

Tags:

java

slf4j

log4j

If a 3rd party dependency logs using a concrete framework (such as log4j), and I want my app to do all logging via slf4j, then is it possible to configure log4j (either in XML, properties file, etc.) to redirect the log messages to the slf4j API? (Which, at runtime, would bind to a concrete slf4j binding of my choice.)

It would just be nice to get all log messages going to the same place through the same API.

I don't even mind if I have to do something a little crazy, like point log4j.properties to some interim code (that I would write), which in turn directs traffic to slf4j!

I just don't know where to start. Thanks in advance!

like image 299
IAmYourFaja Avatar asked Feb 21 '12 20:02

IAmYourFaja


1 Answers

Of course you can and it is thoroughly explained in the documentation:

log4j-over-slf4j

SLF4J ship with a module called log4j-over-slf4j. It allows log4j users to migrate existing applications to SLF4J without changing a single line of code but simply by replacing the log4j.jar file with log4j-over-slf4j.jar, as described below.

It is both hacky and ingenious - you are removing log4j.jar and replacing it with log4j-over-slf4j.jar. The latter JAR mirrors Log4J classes in the same packages, so your application and libraries don't even have to be recompiled. But the new implementations are simply rerouting to SLF4J.

like image 68
Tomasz Nurkiewicz Avatar answered Sep 20 '22 09:09

Tomasz Nurkiewicz