首页 > 解决方案 > Spring errors and warning not coming in logs when dependency of both logback and log4j is there

问题描述

I have a spring boot application where logback is used along with sl4j. Its logging everything perfectly but if some error comes at time of service startup like BeanInitializationException then that is not coming up in console.

I checked the dependency tree then found that log4j jar is also present so i excluded it in perception that problem might be because of conflicts between logback and log4j. But now some third party jar is asking for 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>

this error i am getting

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

标签: spring-bootlog4jslf4jspring-logback

解决方案


尝试使用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


推荐阅读