[英]Problems with slf4j dependency breaking log4j configuration
我有一个项目,我在其中使用 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#exclusiveJCL 但为什么它们都不起作用。 肯定有效果,但是在解决方案2和3之后,spring也开始登录红色。
在尝试盲目地插入不同的 slf4j 绑定和桥接并且没有运气之后 - 我不得不承认我不太了解日志框架,无法自己提出解决方案。 欢迎任何帮助和建议。
原来我的问题是由 flyway-spring-test 扩展引起的,它包含 slf4j-simple 绑定作为依赖项。 即使我自己添加了 log4j 绑定,仍然使用了 slf4j-simple 绑定,因此我的 log4j 配置对使用 slf4j 进行日志记录的代码部分没有影响。
更复杂的是,jOOQ 在加载时检查日志框架,如果找到,则更喜欢 slf4j - 因此添加 slf4j 依赖与 flyway-spring-test 扩展迫使 jOOQ 自动切换日志框架,这给我带来了更多的困惑。
最后,一旦弄清楚问题的原因是什么,修复方法很简单——删除 slf4j-simple 绑定并添加 slf4j-log4j 绑定。 由于版本不匹配问题,我也不得不用更新的版本替换 slf4j-api 依赖项。 我采取的步骤是:
日志现在似乎可以工作了。 最终的依赖树是:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.