slf4j-log4j12 与 log4j 之间的区别
Difference between slf4j-log4j12 vs log4j
在项目的 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-log4j12
和 log4j
之间有什么区别吗?
Log4j 1.2
slf4j-log4j12
提供了 SLF4J 和 Log4j 1.2 之间的桥梁,以便 SLF4J 知道如何使用 Log4j 进行日志记录。
您正在使用 Log4j 1.2。该版本的绑定由 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
不同的绑定 JAR。该绑定由 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.
我将 post 关于这些记录器消息的一些要点。
log4j:
logger.debug("This is log message:" + msg);
log4j
每次评估行时都会连接字符串,即使日志级别低于调试,因此永远不会使用该字符串。
slf4j:
logger.debug("this is log slf4j message",msg);
slf4j
字符串和参数传递给记录器,只有在实际使用日志消息时才会替换它们。
唯一的区别是性能。与 slf4j
.
相比,由于字符串连接,log4j
将花费更多时间
Slf4j:
日志组件的抽象层。
我们可以在不更改代码的情况下更改日志记录。
Log4j:
提供日志记录核心功能的日志记录组件。
总结一下:
<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>
此外,请确保您使用的是 log4j2
属性文件。使用 'log4j.xml' 的错误确实花费了我很多时间
在项目的 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-log4j12
和 log4j
之间有什么区别吗?
Log4j 1.2
slf4j-log4j12
提供了 SLF4J 和 Log4j 1.2 之间的桥梁,以便 SLF4J 知道如何使用 Log4j 进行日志记录。
您正在使用 Log4j 1.2。该版本的绑定由 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
不同的绑定 JAR。该绑定由 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.
我将 post 关于这些记录器消息的一些要点。
log4j:
logger.debug("This is log message:" + msg);
log4j
每次评估行时都会连接字符串,即使日志级别低于调试,因此永远不会使用该字符串。
slf4j:
logger.debug("this is log slf4j message",msg);
slf4j
字符串和参数传递给记录器,只有在实际使用日志消息时才会替换它们。
唯一的区别是性能。与 slf4j
.
log4j
将花费更多时间
Slf4j:
日志组件的抽象层。 我们可以在不更改代码的情况下更改日志记录。
Log4j:
提供日志记录核心功能的日志记录组件。
总结一下:
<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>
此外,请确保您使用的是 log4j2
属性文件。使用 'log4j.xml' 的错误确实花费了我很多时间