Spring 当存在 logback 和 log4j 的依赖时,错误和警告不会出现在日志中

Spring errors and warning not coming in logs when dependency of both logback and log4j is there

我有一个 spring 引导应用程序,其中 logback 与 sl4j 一起使用。它完美地记录了所有内容,但如果在服务启动时出现一些错误,如 BeanInitializationException,那么控制台中不会出现。

我检查了依赖关系树,然后发现 log4j jar 也存在,所以我排除了它,因为我认为问题可能是由于 logback 和 log4j 之间的冲突。但是现在一些第三方 jar 要求 log4j。

    <dependency>
        <groupId>org.owasp.esapi</groupId>
        <artifactId>esapi</artifactId>
        <version>${org.owasp.esapi.version}</version>
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

我收到这个错误

20-08-2019 12:39:46.117 [main] WARN  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext.log - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'connectorController': Unsatisfied dependency expressed through field 'connectorService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'connectorService': Unsatisfied dependency expressed through field 'concurConnector'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getConcurConnector' defined in class path resource [com/oversighttech/application/config/ConnectorConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.oversighttech.Concur.ConcurConnector]: Factory method 'getConcurConnector' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Logger

尝试使用 log4j-over-slf4j 依赖项。它是 log4j API 的替代品,并将调用委托给 slf4j 进行日志记录。

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.28</version>
</dependency>

https://www.slf4j.org/legacy.html#log4j-over-slf4j