Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between Log4j 2 SLF4J Binding and Log4j 2 to SLF4J Adapter

Tags:

java

slf4j

log4j2

As the title, what is the difference between 2 of them.

And when is better to use Log4j 2 SLF4J Binding, and when is better to use Log4j 2 to SLF4J Adapter?

like image 812
Will Avatar asked Jun 18 '15 07:06

Will


People also ask

What is SLF4J binding?

Bindings are basically implementations of a particular SLF4J class meant to be extended to plug in a specific logging framework. By design, SLF4J will only bind with one logging framework at a time. Consequently, if more than one binding is present on the classpath, it will emit a warning.

What version of log4j does SLF4J use?

The Log4j 2 Setup The latest version can be found here: log4j-api, log4j-core, log4j-slf4j-impl. The actual logging configuration adheres to native Log4j 2 configuration. Note that the Logger and LoggerFactory belong to the org. slf4j package.

What is the difference between SLF4J and log4j?

Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.


1 Answers

The log4j2-slf4j binding log4j-slf4j-impl-2.3.jarroutes calls from slf4j to log4j2.

This is the most common usage case. It allows you to code your application with slf4j API but use log4j2 as the underlying implementation.

The log4j2 to slf4j adapter log4j-to-slf4j-2.3.jar does the opposite it routes calls from log4j2 to slf4j.

This is much less commonly used for log4j2 but more so for older frameworks. It is useful whenever you have an existing application coded using the log4j2 framework but wish to use another framework. By using this adapter you route calls from log4j2 to slf4j. slf4j can then route those calls to any compatible implementation as discussed in the first case.

Ideally you always want the first case since it is more straight forward and efficient. However it may require much more refactoring than the second if working with an existing implementation of logging

like image 125
alan7678 Avatar answered Oct 19 '22 17:10

alan7678