简体   繁体   English

Log4j2 / slf4j - 应该从类路径中删除commons-logging.jar吗?

[英]Log4j2/slf4j - Should commons-logging.jar be removed from classpath?

My logging dependencies currently look like this: 我的日志记录依赖项目前如下所示:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-bom</artifactId>
            <version>2.9.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>

I noticed that commons-logging.jar is still in my classpath, for some reason. 我注意到commons-logging.jar仍然在我的类路径中,出于某种原因。 Should I exclude that or doesn't that cause any issues? 我应该排除或不会导致任何问题吗?

I didn't notice any problems so far, but I'm still wondering if that jar would still cause problems somehow. 到目前为止我没有注意到任何问题,但我仍然想知道那个罐子是否仍会以某种方式引起问题。

There are dependencies that use commons-logging . 有依赖项使用commons-logging If it's not present, you'll get NoClassDefFoundErrors when they attempt to log. 如果它不存在,那么当他们尝试登录时,你将获得NoClassDefFoundErrors If there were a possibility to have those not even try to use the dependency, it wouldn't be a problem. 如果有可能让那些甚至不尝试使用依赖项,那就不会有问题。 However that's not very likely. 然而,这不太可能。

However, if they use commons-logging but you're using SLF4J , then there's a problem. 但是,如果他们使用commons-logging而你正在使用SLF4J ,则会出现问题。 They're logging in the wrong place (from your point of view). 他们在错误的地方登陆(从你的角度来看)。 This is where logging bridges come to work. 这是伐木桥开始工作的地方。 They implement the public API of different logging frameworks, but redirect the logging to what you're using. 它们实现了不同日志框架的公共API,但是将日志记录重定向到您正在使用的日志。

For SLF4J there are several bridges (both ways), so instead of bringing in commons-logging , you bring in jcl-over-slf4j . 对于SLF4J ,有几个桥 (两种方式),所以不是引入commons-logging ,而是引入jcl-over-slf4j Libraries will think they're using commons-logging , when they're actually using SLF4J (which then uses an actual logging implementation like Logback). 当他们实际使用SLF4J (然后使用像Logback这样的实际日志记录实现 )时,库会认为他们正在使用commons-logging

Easy, huh? 好吗,对吧? ;) ;)

Yes, exclude the commons-logging dependency and add the log4j-jcl bridge instead: 是的,排除commons-logging依赖项并改为添加log4j-jcl桥:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
</dependency>

SLF4J API is also redundant because it is already a transitive dependency of the log4j-slf4j-impl binding. SLF4J API也是冗余的,因为它已经是log4j-slf4j-impl绑定的传递依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM