Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using log4j for logging with logback [closed]

I want to use log4j(not slf4j) for logging along with logback.jar. Is there anyway doing that? I am working on a web service that consumes log4j and I am intended to use logback.jar along with that.

like image 888
Anonymous Avatar asked Oct 27 '25 13:10

Anonymous


1 Answers

The slf4j project provides some "bridge" jars for dealing with legacy logging. According to http://www.slf4j.org/legacy.html#log4j-over-slf4j:

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.

How does it work?


The log4j-over-slf4j module contains replacements of most widely used log4j classes, namely org.apache.log4j.Category, org.apache.log4j.Logger, org.apache.log4j.Priority, org.apache.log4j.Level, org.apache.log4j.MDC, and org.apache.log4j.BasicConfigurator. These replacement classes redirect all work to their corresponding SLF4J classes.

To use log4j-over-slf4j in your own application, the first step is to locate and then to replace log4j.jar with log4j-over-slf4j.jar. Note that you still need an SLF4J binding and its dependencies for log4j-over-slf4j to work properly.

In most situations, replacing a jar file is all it takes in order to migrate from log4j to SLF4J.

Note that as a result of this migration, log4j configuration files will no longer be picked up. If you need to migrate your log4j.properties file to logback, the log4j translator might be of help. For configuring logback, please refer to its manual.

When does it not work?


The log4j-over-slf4j module will not work when the application calls log4j components that are not present in the bridge. For example, when application code directly references log4j appenders, filters or the PropertyConfigurator, then log4j-over-slf4j would be an insufficient replacement for log4j. However, when log4j is configured through a configuration file, be it log4j.properties or log4j.xml, the log4j-over-slf4j module should just work fine.

In your case, what you would need to do is to add log4j-over-slf4j together with the log4j12, logback-classic and logback-core JARs. You would also need to reimplement the relevant parts of the log4j logging config file in the logback configs.

like image 80
Stephen C Avatar answered Oct 30 '25 14:10

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!