从我在示例 spring pom.xml 文件中看到的是,它们为 slf4j 和 log4j 添加了一些条目,并且不知何故,当您在 spring 应用程序中使用 log4j 时,它将被 slf4j 库包装。
有人可以向我解释一下这是如何神奇地发生的吗?
最佳答案
Spring
仍然使用 commons-logging
进行所有内部日志记录(向后兼容)。
如果您希望使用其他一些日志框架 (log4j
),那么您需要将 commons logging
的调用桥接 到您选择的框架。否则,您将不得不维护多个日志记录配置。
slf4j
充当各种日志框架(jul
、log4j
、jcl
、logback
) 并允许您在部署时插入所需的日志框架。
您提供 slf4j's
桥接实现,而不是使用第三方框架强加的日志框架实现,该实现就像真实的东西,但实际上只是将日志调用转发到 slf4j
或其具体绑定(bind)。
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>
这与Spring容器无关,也与依赖注入(inject)无关,它是纯类路径,类加载器的东西......
关于spring - 如何简单地将 slf4j 添加到 pom.xml 包装 log4j?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8670069/