12

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!

Splioo
  • 370
  • 3
  • 11

3 Answers3

9

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

Avi
  • 1,231
  • 10
  • 23
3

Please correct me if I am wrong - will try to answer my question.

I just had a look on mvnrepository.org - there is no log4j-to-slf4j.jar beneath version 2.x and from the link I pasted above it is stated that this is an Adapter of log4j-2.x and not 1.x. Also its provided by the Apache Foundation.

In contrary the log4j-over-slf4j.jar is documented on slf4j.org and does redirect log4j-1.x records to the slf4j-api.

So I think that I mixed apples and pears.

Splioo
  • 370
  • 3
  • 11
  • I think you got it. The biggest difference is that log4j has two versions. `log4j-over-slf4j` helps migrating legacy code to a new logging framework by taking care of sending messages to slf4j (which then forwards it further to whatever framework you are using for logging). `log4j-to-slf4j` kind of does the same, but is not specifically made for legacy project migrating (still can be used for that, I guess) Don't forget: If there's a 'logging' within the GAV identifier, log4j2.X is meant. Made it easier for me to distinguish – metters Apr 14 '20 at 05:35
1

log4j-to-slf4j.jar: The Log4j 2 to SLF4J Adapter allows applications coded to the Log4j 2 API to be routed to SLF4J.

log4j-over-slf4j.jar : 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

J John
  • 299
  • 1
  • 3
  • 15