spring 如何简单地将 slf4j 添加到 pom.xml 包装 log4j?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8670069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:50:39  来源:igfitidea点击:

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

springlog4jslf4j

提问by codecompleting

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.

从我在示例 spring pom.xml 文件中看到的是,它们为 slf4j 和 log4j 添加了一些条目,并且不知何故,当您在 spring 应用程序中使用 log4j 时,它将被 slf4j 库包装。

Can someone explain to me how this magically happens?

有人可以向我解释这是如何神奇地发生的吗?

回答by Bruno Devi?

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

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

slf4jacts 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作为一个简单的门面各种日志框架(jullog4jjcllogback),并允许您在部署时所需的日志框架插头。

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

而不是使用由第三方框架强加的日志框架实现,您提供的slf4j's桥实现就像真实的东西,但实际上只是将日志调用转发到slf4j或其具体绑定。

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

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

<!-- 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...

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

Please see thefollowinglinks for further details.

请参阅下面进一步的细节链接。

回答by kan

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

slf4j是一个日志 API,它什么也不做,只是一堆接口。log4j是一个日志系统,具有具体的类。有一个slf4j-log4j库使用 log4j 作为 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.

一些项目明确依赖于 log4j,它们称为具体类。因此,您不能将其他后端(例如 logback 或 jul 或 apache commons 或其他)用于您仅使用 slf4j API 明智地制作的项目。

There is a trickto 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.

一个技巧可以通过模拟实现(桥接器)替换 log4j 类,它只是简单地将所有调用重定向到 sl4j。在 maven 中,您只需声明一个具有非常高版本号的依赖项,并且此模拟被视为超现代 log4j 库。