2
votes

I am trying to wrap my head around all the different logging tools (log4j, slf4j, logback, jcl, etc) and what they all do.

I understand that slf4j is a facade to the different logging tools making it easy to switch between any logging tools. But I am confused when I get to the topic of logback. I understand logback is a successor to log4j and from this post it uses the words "natively implements"; what does that mean exactly. From what I'm understanding is that logback is the same thing as slf4j.....so is it a facade as well? I'm getting mixed descriptions of logback being a back end logging tool and a face just the same as slf4j.

I tried a small test project to understand how it works. So in my maven pom I put:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>

Then in the code I put:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Foo {
    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);
}

So does this implementation mean that slf4j is a facade for logback and that logback is a back end logging tool the same as log4j and jcl?

2

2 Answers

5
votes

It means that all of the methods defined in SLF4J's interfaces are implemented directly in Logback's classes. There's no additional layer to "translate" SLF4J calls to the actual logging implementation.

This can be easily seen when you look at org.slf4j.Logger interface and ch.qos.logback.classic.Logger class which implements it. It fits like a glove.

0
votes

Short answer is Logback is not a facade but a backend implementation similar to (but better than) log4j, java.util.logging, jcl and others. Read on for the detailed answer below:

The explanation below add more detail to what @Kamayan has explained in his answer. Breaking down your question statement by statement.

I understand that slf4j is a facade to the different logging tools making it easy to switch between any logging tools.

In simpler terms, it means that you always create your logger as well as call log methods of the SLF4J API without worrying about the actual implementing APIs like log4j or logback etc. The actual implementation comes into picture only at the runtime when the behavior of logging is determined by the logging implementation. To map this with a simple code snippet:

private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);
LOGGER.debug("A debug log");

Here Logger is org.slf4j.Logger and LoggerFactory is org.slf4j.LoggerFactory.

I understand logback is a successor to log4j and from this post it uses the words "natively implements"; what does that mean exactly.

Logger class in logback-classic implements all the methods that are present in Logger class of the SLF4J API (which is not the case with log4j, jcl etc.) This means that you incur zero overhead when invoking an SLF4J logger with logback-classic as the underlying implementation.

From what I'm understanding is that logback is the same thing as slf4j.....so is it a facade as well?

NO, it is not a facade. A nicer way to say this would be that it is closer to the slf4j facade than its peers - log4J, jcl etc.

Here is a very nice article from logback project that draws comparison between logback and other logging implementations.