125

I am little bit confused by these three logger libraries. It seems like that they can do the similar thing in Java logging...

5
  • 1
    They do similar things. They have different APIs. I would use the one you like best. BTW have a look at log4j2 rather than log4j. Commented Sep 18, 2016 at 21:44
  • 4
    @PeterLawrey With the exception of slf4j, which isn't a logger in itself (it is a facade to provide a consistent api to another logger). Commented Sep 18, 2016 at 21:45
  • 6
    Why the question gets 2negative votes? Commented May 11, 2017 at 5:54
  • 4
    Some benchmark here: loggly.com/blog/benchmarking-java-logging-frameworks
    – Foyta
    Commented Aug 24, 2018 at 14:49
  • Welcome to the bazaar !
    – Arfur Narf
    Commented May 13, 2023 at 2:08

3 Answers 3

177

Check out their home pages:

SLF4J - The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction1 for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.

1) It is not itself a logging library, but a generic interface to one of many logging libraries.

Log4j 1.2 - Welcome to Apache log4j, a logging library for Java.

Logback - Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.

Log4j 2 - Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback's architecture.

At least, that's what they all say of themselves.

4
  • 7
    So with SLF4J as a facade, we can use either log4j or logback or <any other compatible logging framework>. Is it?
    – Shekhar
    Commented Jan 13, 2020 at 15:12
  • 4
    @Shekhar yes that's correct
    – Atefeh
    Commented Sep 22, 2021 at 12:33
  • It is not quite correct that SLF4J is not a logging library. It is a logging library, in particular when using the slf4j-simple provider. Commented May 2, 2023 at 21:36
  • 1
    @PhilippeCloutier No one considers SLF4J a "logging library". It is an API. The Simple Logger it provides is rarely used.
    – rgoers
    Commented May 11, 2023 at 4:44
12

This link : https://medium.com/@krishankantsinghal/logback-slf4j-log4j2-understanding-them-and-learn-how-to-use-d33deedd0c46

Explains the differences in detail.

Quoting from there

Slf4j

So Basically Simple Logging Facade for Java serves as a simple facade or abstraction for various logging frameworks allowing the end user to plug in the desired logging framework at deployment time.

log4j2

Log4j,Logback and java.util.Logger are logging libraries which actually write the logs and have their own pros and cons. As industry standards are Log4j2 and logback

I would recommend going through the blog. It provides all the glory details how both are used with adapter.

1
  • The article you reference has some valuable information but is of low quality. It includes incorrect information (including a major mistake reported in a comment). It has some interesting graphics, but I assume some are in violation of SLF4J's copyrights. I'm therefore downvoting this to favor Andreas's far from perfect but IMO better answer. Commented May 2, 2023 at 20:53
11

Your question is far from easy, and only covers some of the main elements of the Java ecosystem's ridiculously complicated logging. I do not know that ecosystem enough to fully describe it, but describing the current situation well and how it came to be would surely require days/weeks of work to write what would resemble a book.

Here is a very short, surely simplistic answer focusing on the elements you mentioned.

Log4j appeared as a flexible logging library in 2001.

Logback was developed as a more powerful alternative to Log4j from 2006 to 2011. The Simple Logging Facade for Java (SLF4J) was developed between 2005 and 2006 as a superior alternative to [Apache/Jakarta] Commons Logging (JCL). Logback and SLF4J are complementary (they can be used together, or not).

Perhaps because Logback and SLF4J are to a small degree commercial, the Apache Foundation released in 2014 a major upgrade to Log4j. Log4j 2 is somewhat comparable to Logback+SLF4J, in that it provides a facade API (log4j-api, comparable to SLF4J) as well as an implementation (log4j-core, comparable to Logback).

If the above seems to contradict other sources, that may be because Log4j version 1 was very different and remains in use, so sources describing "Log4j" may in fact still describe Log4j 1.

6
  • 1
    Nice answer! Log42 (or better Log4j2 API+Log4j2 Core) is even exactly as SLF4J+Logback: both are developed by a single entity (Ceki Gülcü for SLF4J+Logback, the Apache Logging Services PMC for Log4j2 API+Log4j2 Core), both backends are mainly driven by changes in their API (and viceversa). Also both APIs can work with both backends. Commented May 3, 2023 at 10:39
  • 1
    Log4j and SLF4J+Logback are comparable, but while Log4j is one solution, I would consider Logback and SLF4J as highly complementary products. Unlike Log4j, SLF4J for instance features slf4j-simple, which allows to use it alone, without Logback. Whereas Logback offers logback-classic, a module which enables integration with SLF4J. It may be a nuance, but I'd rather not oversimplify, because the topic is already complicated enough! Commented May 4, 2023 at 14:37
  • 1
    That is IMHO a misconception, due to the fact that Log4j2 API and Log4j2 Core share a common name. Like SLF4J, Log4j2 API has a simple binding (it is included in log4j-api) and bindings to all major logging frameworks (i.e. Logback, java.util.logging and JBoss LogManager). It only lacks a binding to Log4j 1.x for obvious reasons. Commented May 4, 2023 at 16:15
  • Hum, interesting. If log4j-api includes "a simple binding" to itself, I guess that's more of an implementation that a binding proper, but regardless, I can't find any documentation of that. It's not that I really question your say, but surely such a feature would be documented somewhere? Commented May 5, 2023 at 17:58
  • Documentation is hard to come by. To configure a SimpleLoggerContext you use system properties, which are documented here. From time to time a question like this pops up. Commented May 5, 2023 at 22:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.