【问题标题】:LoggerFactory is not a Logback LoggerContext but Logback is on the classpathLoggerFactory 不是 Logback LoggerContext 但 Logback 在类路径上
【发布时间】:2015-08-27 19:49:35
【问题描述】:

我认为 spring-boot-starter-security 中的某些模块与 log4j 冲突,但我不知道是哪一个。

我的gradle依赖如下:

compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security"){
    exclude module: "spring-boot-starter-logging"
}

compile "org.apache.logging.log4j:log4j-api"
compile "org.apache.logging.log4j:log4j-core"
compile "org.apache.logging.log4j:log4j-slf4j-impl"
compile('org.apache.poi:poi:3.10.1')
compile('org.apache.poi:poi-ooxml:3.10.1')
testCompile("junit:junit")

【问题讨论】:

    标签: java logging spring-security spring-boot dependency-management


    【解决方案1】:

    我想通了

    compile("org.springframework.boot:spring-boot-starter-security"){
        exclude module: "spring-boot-starter-logging"
        exclude module: "logback-classic"
    }
    compile("org.springframework.boot:spring-boot-starter-thymeleaf"){
        exclude module: "logback-classic"
    }
    

    【讨论】:

    • 也适用于 webflux compile(group: 'org.springframework.boot', name: 'spring-boot-starter-webflux', version: '2.2.6.RELEASE') { // exclude modules }
    【解决方案2】:

    maven 的相同解决方案:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>1.5.1.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    【讨论】:

      【解决方案3】:

      使用 IDEA “Show Dependencies” 或 mvn dependency:tree -Dverbose 查找所有 logback-classic jar 文件,并排除它们。

      【讨论】:

        【解决方案4】:

        对我来说,这个问题只在执行 maven-surefire-plugin 时出现。不知何故,Logback 存在于类路径中,即使它没有添加到项目依赖项中。我添加了这样的修复:

        <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <version>${maven-surefire-plugin.version}</version>
             <configuration>
                <systemPropertyVariables>
                    <org.springframework.boot.logging.LoggingSystem>
                        org.springframework.boot.logging.log4j2.Log4J2LoggingSystem
                    </org.springframework.boot.logging.LoggingSystem>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        

        你也可以使用

        mvn clean install -Dorg.springframework.boot.logging.LoggingSystem=org.springframework.boot.logging.log4j2.Log4J2LoggingSystem
        

        【讨论】:

        • 会尽我所能:在我的情况下,这是因为 gradle 插件部分中的“java-gradle-plugin”
        【解决方案5】:

        我的问题是:删除 Logback 或从 log4j-slf4j-impl-2.12.0.jar 加载的竞争实现类 org.apache.logging.slf4j.Log4jLoggerFactory

        我加了

        configurations {
            all {
                exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
                exclude group: 'ch.qos.logback', module: 'logback-classic'
                exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
            }
        }
        

        到我的 gradle.build 并且还用最新版本刷新了所有项目依赖项并解决了问题。

        【讨论】:

          【解决方案6】:

          根据this bug,捆绑且不可删除的 Kotlin 插件(提醒我将 U2 的专辑强制包含在 iOS 中)包括 org.gradle.kotlin.kotlin-dsl,这反过来会使用不兼容的日志库(即使已禁用)污染类路径。

          我通过在首选项中将跑步者从“IntelliJ IDEA”更改为“Gradle”解决了这个问题:

          • 构建、执行、部署
            • 构建工具
              • 梯度
                • 使用 -> Gradle 构建运行
                • 使用 -> Gradle 运行测试

          【讨论】:

            【解决方案7】:

            根据这个documentation,Spring Boot因为web starter自动配置了Log Back库。您应该排除 spring-boot-starter-logging 来解决问题。

            要排除 gradle 项目上的默认 LogBack 配置,

            configurations {
                all*.exclude module : 'spring-boot-starter-logging'
                all*.exclude module : 'logback-classic'
            }
            

            【讨论】:

            • 感谢您的回答!但是您应该考虑 editting 它包含您所包含的参考文献的引用。如果您链接到的参考资料消失,这将使您的答案更加可靠。
            猜你喜欢
            • 2017-11-09
            • 1970-01-01
            • 2018-11-22
            • 1970-01-01
            • 2019-05-21
            • 1970-01-01
            • 2016-04-20
            • 2015-11-30
            • 1970-01-01
            相关资源
            最近更新 更多