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?
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.
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.
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.
The log4j2-slf4j binding log4j-slf4j-impl-2.3.jar
routes 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With