本文介绍了简单地将slf4j添加到pom.xml中如何包装log4j?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我在示例spring pom.xml文件中看到的内容来看,它们为slf4j和log4j添加了一些条目,并且以某种方式在Spring应用程序中使用log4j时,它将被slf4j库包装.

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 wrapped by slf4j library.

有人可以向我解释这是怎么发生的吗?

Can someone explain to me how this magically happens?

推荐答案

Spring仍将commons-logging用于所有内部日志记录(向后兼容).如果您希望使用其他日志记录框架(log4j),则需要桥接commons logging到您选择的框架的调用.否则,您将必须维护多个日志记录配置.

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充当各种日志记录框架(jullog4jjcllogback)的简单外观,并允许您在部署时插入所需的日志记录框架.

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.

代替使用第三方框架强加的日志记录框架实现,您提供了slf4j's桥实现,其作用与真实情况类似,但实际上只是将日志记录调用转发给slf4j或其具体绑定.

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.

Maven pom.xml的日志记录部分通常如下所示:

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>

这与Spring容器或依赖项注入无关,它是纯类路径,类加载器之类的东西……

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.

这篇关于简单地将slf4j添加到pom.xml中如何包装log4j?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 03:24