Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between log4j-to-slf4j and log4j-over-slf4j

I dont really get the difference between log4j-to-slf4j.jar and log4j-over-slf4j.jar. From what I read on the internet

log4j-to-slf4j.jar: (https://logging.apache.org/log4j/2.x/log4j-to-slf4j/index.html)

"The Log4j 2 to SLF4J Adapter allows applications coded to the Log4j 2 API to be routed to SLF4J. Use of this adapter may cause some loss of performance as the Log4j 2 Messages must be formatted before they can be passed to SLF4J. With Log4j 2 as the implementation these would normally be formatted only when they are accessed by a Filter or Appender."

log4j-over-slf4j.jar (http://www.slf4j.org/legacy.html)

"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. 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."

From what I understand both solve the same problem. But where is exactly the difference? Bridge vs Adapter?

Thanks a lot!

like image 395
Splioo Avatar asked Sep 13 '25 23:09

Splioo


1 Answers

In Log4j 2 the API is separate from the implementation.

log4j-api.jar: (https://logging.apache.org/log4j/2.x/log4j-api/index.html)

The Log4j 2 API provides the interface that applications should code to and provides the adapter components required for implementers to create a logging implementation.

log4j-core.jar: (https://logging.apache.org/log4j/2.x/log4j-core/index.html)

The Apache Log4j Implementation

This decoupling allows using the log4j API and an unrelated backing logging implementation, much like slf4j is used.

Where does log4j-to-slf4j come in? Often when dependencies using the log4j API are introduced. For instance, an application may use slf4j + a non-log4j logging implementation and then add a dependency that uses the log4j API. With log4j-to-slf4j the application's slf4j log messages and the dependencie's log4j log messages will all be routed to the same place (the logging implementation).

Meanwhile, log4j 1 doesn't have the API/implementation separation. The previously outlined scenario may still arise. In that situation log4j-over-slf4j can be used as a drop in replacement for log4j.jar (i.e. log4j 1).

How does it work?

The log4j-over-slf4j module contains replacements of most widely used log4j 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

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

-- http://www.slf4j.org/legacy.html

In summary, they accomplish the same thing but do so very differently and are used for different log4j versions (1 vs 2).

like image 59
Avi Avatar answered Sep 15 '25 21:09

Avi