【问题标题】:Problems with slf4j dependency breaking log4j configurationslf4j 依赖关系破坏 log4j 配置的问题
【发布时间】:2015-09-06 17:13:58
【问题描述】:

我有一个项目,我使用 log4j 进行日志记录,它曾经工作得很好。现在我不得不添加一个使用 slf4j 的依赖项,突然日志记录不再按预期工作 - 代码的某些部分忽略了我的 log4j 配置并在 Eclipse 控制台中以红色登录。

这是日志记录工作时的依赖树:

my.package.program:program-database:jar:0.3.0
+- my.package.program:program-common:jar:0.3.0:compile
|  +- org.apache.commons:commons-lang3:jar:3.4:compile
|  \- commons-io:commons-io:jar:2.4:compile
+- org.springframework:spring-tx:jar:4.1.0.RELEASE:compile
|  \- org.springframework:spring-core:jar:4.1.0.RELEASE:compile
|     \- commons-logging:commons-logging:jar:1.1.3:compile
+- org.springframework:spring-jdbc:jar:4.1.0.RELEASE:compile
+- org.jooq:jooq:jar:3.6.2:compile
+- org.jooq:jooq-meta:jar:3.6.2:compile
+- org.jooq:jooq-codegen:jar:3.6.2:compile
+- postgresql:postgresql:jar:9.1-901-1.jdbc4:compile
+- org.springframework:spring-beans:jar:4.1.0.RELEASE:compile
+- org.springframework:spring-context:jar:4.1.0.RELEASE:compile
|  +- org.springframework:spring-aop:jar:4.1.0.RELEASE:compile
|  |  \- aopalliance:aopalliance:jar:1.0:compile
|  \- org.springframework:spring-expression:jar:4.1.0.RELEASE:compile
+- org.apache.logging.log4j:log4j-api:jar:2.1:compile
+- org.apache.logging.log4j:log4j-core:jar:2.1:compile
+- org.apache.logging.log4j:log4j-1.2-api:jar:2.1:compile
+- junit:junit:jar:4.11:test
|  \- org.hamcrest:hamcrest-core:jar:1.3:test
+- org.springframework:spring-test:jar:4.1.0.RELEASE:test

这是我添加 flyway 插件并破坏日志记录后的树:

my.package.program:program-database:jar:0.3.0
+- my.package.program:program-common:jar:0.3.0:compile
|  +- org.apache.commons:commons-lang3:jar:3.4:compile
|  \- commons-io:commons-io:jar:2.4:compile
+- org.springframework:spring-tx:jar:4.1.0.RELEASE:compile
|  \- org.springframework:spring-core:jar:4.1.0.RELEASE:compile
|     \- commons-logging:commons-logging:jar:1.1.3:compile
+- org.springframework:spring-jdbc:jar:4.1.0.RELEASE:compile
+- org.jooq:jooq:jar:3.6.2:compile
+- org.jooq:jooq-meta:jar:3.6.2:compile
+- org.jooq:jooq-codegen:jar:3.6.2:compile
+- postgresql:postgresql:jar:9.1-901-1.jdbc4:compile
+- org.flywaydb.flyway-test-extensions:flyway-spring-test:jar:3.2.1:test
|  +- org.slf4j:slf4j-api:jar:1.5.6:test
|  +- org.slf4j:slf4j-simple:jar:1.5.6:test
|  +- org.flywaydb:flyway-core:jar:3.2.1:test
|  \- commons-dbcp:commons-dbcp:jar:1.4:test
|     \- commons-pool:commons-pool:jar:1.5.4:test
+- org.springframework:spring-beans:jar:4.1.0.RELEASE:compile
+- org.springframework:spring-context:jar:4.1.0.RELEASE:compile
|  +- org.springframework:spring-aop:jar:4.1.0.RELEASE:compile
|  |  \- aopalliance:aopalliance:jar:1.0:compile
|  \- org.springframework:spring-expression:jar:4.1.0.RELEASE:compile
+- org.apache.logging.log4j:log4j-api:jar:2.1:compile
+- org.apache.logging.log4j:log4j-core:jar:2.1:compile
+- org.apache.logging.log4j:log4j-1.2-api:jar:2.1:compile
+- junit:junit:jar:4.11:test
|  \- org.hamcrest:hamcrest-core:jar:1.3:test
+- org.springframework:spring-test:jar:4.1.0.RELEASE:test

我认为问题来自这样一个事实,即 spring 包含 commons-logging 的传递依赖,并且当 flyway 引入 slf4j 时,cli 获得优先级并且我的 log4j 配置被某些库忽略(等 jooq 和 flyway 开始以红色登录)。

我尝试了这里提到的所有 3 个选项: http://www.slf4j.org/faq.html#excludingJCL 但不知何故,他们都没有工作。肯定有效果,但是在解决方案2和3之后,spring也开始登录红色了。

在尝试盲目地插入不同的 slf4j 绑定和网桥并且没有运气之后 - 我不得不承认我对日志框架的了解不足以自己提出解决方案。欢迎任何帮助和建议。

【问题讨论】:

    标签: java maven log4j slf4j flyway


    【解决方案1】:

    原来我的问题是由包含 slf4j-simple 绑定作为依赖项的 flyway-spring-test 扩展引起的。即使我自己添加了 log4j 绑定,仍然使用 slf4j-simple 绑定,因此我的 log4j 配置对使用 slf4j 进行日志记录的代码部分没有影响。

    更复杂的是,jOOQ 在加载时检查日志框架,如果找到则更喜欢 slf4j - 因此添加 slf4j 依赖和 flyway-spring-test 扩展迫使 jOOQ 自动切换日志框架,从而为我。

    最后,一旦明确了问题的原因,修复就很简单——删除 slf4j-simple 绑定并添加 slf4j-log4j 绑定。由于版本不匹配问题,我也不得不用更新的版本替换 slf4j-api 依赖项。我采取的步骤是:

    1. 从 flyway-spring-test 依赖项中排除 org.slf4j:slf4j-api
    2. 从 flyway-spring-test 依赖项中排除 org.slf4j:slf4j-simple
    3. 将更新版本的 org.slf4j:slf4j-api 添加到项目中
    4. 将 org.apache.logging.log4j:log4j-slf4j-impl 添加到项目中

    日志记录现在似乎可以工作了。最终的依赖树是:

    my.package.program:program-database:jar:0.3.0
    +- my.package.program:program-common:jar:0.3.0:compile
    |  +- org.apache.commons:commons-lang3:jar:3.4:compile
    |  \- commons-io:commons-io:jar:2.4:compile
    +- org.springframework:spring-tx:jar:4.1.0.RELEASE:compile
    |  \- org.springframework:spring-core:jar:4.1.0.RELEASE:compile
    |     \- commons-logging:commons-logging:jar:1.1.3:compile
    +- org.springframework:spring-jdbc:jar:4.1.0.RELEASE:compile
    +- org.jooq:jooq:jar:3.6.2:compile
    +- org.jooq:jooq-meta:jar:3.6.2:compile
    +- org.jooq:jooq-codegen:jar:3.6.2:compile
    +- postgresql:postgresql:jar:9.1-901-1.jdbc4:compile
    +- org.flywaydb.flyway-test-extensions:flyway-spring-test:jar:3.2.1:test
    |  +- org.flywaydb:flyway-core:jar:3.2.1:test
    |  \- commons-dbcp:commons-dbcp:jar:1.4:test
    |     \- commons-pool:commons-pool:jar:1.5.4:test
    +- org.springframework:spring-beans:jar:4.1.0.RELEASE:compile
    +- org.springframework:spring-context:jar:4.1.0.RELEASE:compile
    |  +- org.springframework:spring-aop:jar:4.1.0.RELEASE:compile
    |  |  \- aopalliance:aopalliance:jar:1.0:compile
    |  \- org.springframework:spring-expression:jar:4.1.0.RELEASE:compile
    +- org.apache.logging.log4j:log4j-api:jar:2.3:compile
    +- org.apache.logging.log4j:log4j-core:jar:2.3:compile
    +- org.apache.logging.log4j:log4j-1.2-api:jar:2.3:compile
    +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.3:compile
    +- org.slf4j:slf4j-api:jar:1.7.12:compile
    +- junit:junit:jar:4.11:test
    |  \- org.hamcrest:hamcrest-core:jar:1.3:test
    +- org.springframework:spring-test:jar:4.1.0.RELEASE:test
    

    【讨论】:

    • 这并不真正准确,因为 Flyway 本身对任何日志框架都没有必需的依赖项。然而,它会在运行时检测到您在类路径中拥有的那个并使用它。
    • 是的,依赖不是直接来自 Flyway - 当我开始在构建中使用 flyway-spring-test 组件时,它被引入了。我会修改我的答案以反映这一点
    • @AxelFontaine:有趣,你是怎么做到的?只有反射?
    • @LukasEder 此处提供所有信息:axelfontaine.com/blog/optional-dependencies.html
    猜你喜欢
    • 2013-11-05
    • 2018-02-21
    • 2019-09-27
    • 2011-11-20
    • 2016-11-02
    • 1970-01-01
    • 2019-07-30
    • 2015-11-01
    • 1970-01-01
    相关资源
    最近更新 更多