How does simply adding slf4j to the pom.xml wrap log4j?

后端 未结 2 650
醉话见心
醉话见心 2020-12-23 21:53

From what I have seen in example spring pom.xml files is that they add a few entries for slf4j and log4j and somehow when you use log4j in your spring application it will be

相关标签:
2条回答
  • 2020-12-23 22:17

    Spring still uses commons-logging for all the internal logging (backwards compatibility). If you wish to use some other logging framework (log4j) then you need to bridge the calls from commons logging to your framework of choice. Otherwise you will have to maintain multiple logging configurations.

    slf4j acts as a simple facade for various logging frameworks (jul, log4j, jcl, logback) and allows you to plug in the desired logging framework at deployment time.

    Instead of using the logging framework implementation that is imposed by the third party framework you provide the slf4j's bridge implementation that acts like the real thing but really just forwards the logging calls to slf4j or its concrete binding.

    Logging section of Maven pom.xml usually looks like this:

    <!-- remove the real commons-logging from classpath -->
    <!-- declare as provided or exclude from spring jars -->
    <dependency>
        <artifactId>commons-logging</artifactId>
        <groupId>commons-logging</groupId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- add slf4j interfaces to classpath -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.4</version>
        <scope>compile</scope>
    </dependency>
    
    <!-- add commons logging to slf4j bridge to classpath --> 
    <!-- acts as jcl but routes commons-logging calls to slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.6.4</version>
        <scope>runtime</scope>
    </dependency>
    
    <!-- add log4j binding to classpath -->
    <!-- routes slf4j calls to log4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.4</version>
        <scope>runtime</scope>
    </dependency>
    
    <!-- add log4j to classpath -->
    <!-- does the logging -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    

    This has nothing to do with the Spring container nor dependency injection, it is pure classpath, classloader stuff...

    Please see the following links for further details.

    0 讨论(0)
  • 2020-12-23 22:26

    slf4j is a logging API, which doesn't do anything, just bunch of interfaces. log4j is a logging system, with concrete classes. there is a slf4j-log4j library which uses log4j as a backend for the slf4j API.

    Some projects explicitly depend on log4j, they call concrete classes. So, you cannot use another backend (e.g. logback or j.u.l or apache commons or whatever) for your project which you wisely made using the slf4j API only.

    There is a trick to substitute log4j classes by a mock implementation (the bridge) which just simply redirects all calls to the sl4j. In maven you just declare a dependency with very high version number and this mock considered as ultra-modern log4j library.

    0 讨论(0)
提交回复
热议问题