13

In a project's pom.xml I am seeing a dependency like below

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

Can someone let me know what is the difference between slf4j-log4j12 & log4j ?

tuk
  • 5,941
  • 14
  • 79
  • 162
  • 1
    slf4j is a facade for loggers. You can switch without changing the code. log4j is one the loggers you can use there. – Jack Flamp Nov 30 '17 at 13:34
  • @JackFlamp - Yes but what is the difference between `slf4j-log4j12` & `log4j`. If both are same then is there a need to add the dependency of both ? – tuk Nov 30 '17 at 13:37
  • 1
    https://stackoverflow.com/a/4311178/3959856 – Jack Flamp Nov 30 '17 at 13:40
  • 1
    Thanks @JackFlamp for the link. My doubt is cleared now `slf4j-log4j12` is a binding for log4j which binds slf4j to the underlying logging framework in this case log4j. `slf4j-api` just defines the logging api. – tuk Nov 30 '17 at 13:46

4 Answers4

12

Log4j 1.2

slf4j-log4j12 provides a bridge between SLF4J and Log4j 1.2 so that SLF4J knows about how to log using Log4j.

You are using Log4j 1.2. That version's binding it is maintained by the SLF4J project. Here is a summary from the SLF4J docs:

SLF4J supports various logging frameworks. The SLF4J distribution ships with several jar files referred to as "SLF4J bindings", with each binding corresponding to a supported framework.

slf4j-log4j12-1.7.28.jar

Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.

Log4j 2

If you are using Log4j 2 or later, you will need a different binding JAR than slf4j-log4j12. That binding is maintained by the Log4j project. According to the Log4j docs:

The Log4j 2 SLF4J Binding allows applications coded to the SLF4J API to use Log4j 2 as the implementation.

You must provide both dependencies if you want SLF4J to route logging to Log4j. Again, from the Log4j 2 docs:

Simply include the Log4j 2 SLF4J Binding jar along with the Log4j 2 jars and SLF4J API jar to cause all SLF4J logging to be handled by Log4j 2.

AndrewF
  • 1,827
  • 13
  • 25
glytching
  • 44,936
  • 9
  • 114
  • 120
  • 5
    Note that the "12" in `slf4j-log4j12` indicates that this is for Log4j 1.2, not Log4j 2. If you want to hook it to Log4j 2, then use `org.apache.logging.log4j:log4j-slf4j-impl`, not SLF4J's `slf4j-log4j12`. – AndrewF May 04 '19 at 04:06
  • The docs link is misleading, as it points to Apache's Log4J 2.x documentation, which as @AndrewF points out, describes the `log4j-slf4j-impl` binding. The only documentation I could find regarding `slf4j-log4j12` is in the [project's POM](https://search.maven.org/artifact/org.slf4j/slf4j-log4j12/1.7.25/jar), indicating the name of the project is `SLF4J LOG4J-12 Binding`. Log4J 1.2 is quite a way behind the Log4J 2.x product that @glytching references. – chaserb May 11 '20 at 23:27
  • I have proposed an edit that rewrites the answer to clearly differentiate between 1.2 and 2. – AndrewF May 23 '20 at 22:26
4

To summarize:

 <dependency> <!--Facade for logging systems-->
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.7.25</version>
 </dependency>

 <dependency> <!--Log4j 2 implementation for slf4j-->
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-slf4j-impl</artifactId>
   <version>2.12.0</version>
 </dependency>

In addition make sure that you are using a log4j2 properties file. The mistake of using 'log4j.xml' did cost me quite some time

trallnag
  • 2,041
  • 1
  • 17
  • 33
  • Exactly, thank you so much, I was having the same issue with log4j.xml file until I created the properties file. So is it a bug? – yonikawa Jan 03 '22 at 18:21
0

I'll post some points regarding these logger message.

log4j:

logger.debug("This is log message:" + msg);

log4j string is concatenated every time the line is evaluated even if log level is lower than debug so the string will never be used.

slf4j:

logger.debug("this is log slf4j message",msg);

slf4j string and parameters are passed through to the logger which only substitutes them if the log message is actually to be used.

The only difference is the performance. Where log4j will take more time because of string concatenation compare to slf4j.

Arvind Katte
  • 995
  • 2
  • 10
  • 20
  • I think that you are comparing two things in a way they cannot be compared. In slf4j you can also perform `logger.debug("This is log message:" + msg);` so the performance difference is not a valid point in this case. – RCaetano Feb 14 '19 at 15:42
  • The question was about the difference between two artifacts. This was not a question about usage, best practices, APIs, or even the libraries. – AndrewF May 04 '19 at 04:14
-1

Slf4j:

An abstract layer for the logging component. We can change logging at point of time without much changes in code.

Log4j:

A logging component which provides core functionalities of logging.