java - slf4j-log4j12 与 log4j 之间的区别

标签 java log4j slf4j slf4j-api

在项目的 pom.xml 中,我看到如下所示的依赖项

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

有人可以告诉我 slf4j-log4j12log4j 之间有什么区别吗?

最佳答案

Log4j 1.2

slf4j-log4j12 提供了 SLF4J 和 Log4j 1.2 之间的桥梁,以便 SLF4J 知道如何使用 Log4j 进行日志记录。

您正在使用 Log4j 1.2。该版本的绑定(bind)由 SLF4J 项目维护。这是来自 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

如果您使用的是 Log4j 2 或更高版本,则需要与 slf4j-log4j12 不同的绑定(bind) JAR。该绑定(bind)由 Log4j 项目维护。 According to the Log4j docs :

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

如果您希望 SLF4J 将日志记录路由到 Log4j,则必须提供这两个依赖项。同样,来自 Log4j 2 文档:

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.

关于java - slf4j-log4j12 与 log4j 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47574644/

相关文章:

java - 我可以在 ant 目标中声明属性值并将该值用于另一个目标吗?

java - 如何在 Tomcat 5.5 中控制 webapp 中第三方组件的日志记录?

java - 关闭 hibernate 日志到控制台

java - 如何提高上传到 Firebase 存储的速度?

java - writer.dispose() 未按预期工作

Java - InputStreamReader 的替代品?

java - log4j2 - 在执行期间更改输出文件

java - apache commons 日志记录是否支持占位符?

java - Log4j2 属性文件 JDBC 附加程序 - 将上下文变量转换为 uuid

java - 没有找到类“org.slf4j.LoggerFactory