Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java slf4j依赖项破坏log4j配置的问题_Java_Maven_Log4j_Slf4j_Flyway - Fatal编程技术网

Java slf4j依赖项破坏log4j配置的问题

Java slf4j依赖项破坏log4j配置的问题,java,maven,log4j,slf4j,flyway,Java,Maven,Log4j,Slf4j,Flyway,我有一个项目,我使用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.com

我有一个项目,我使用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日志的可传递依赖项,当flyway引入slf4j时,cli获得优先权,我的log4j配置被一些库忽略(etc jooq和flyway开始以红色登录)

我尝试了这里提到的所有3个选项: 但是为什么他们都不工作。当然有影响,但在解决方案2和3之后,spring也开始以红色登录


在尝试盲目地插入不同的slf4j绑定和桥之后,我没有运气——我不得不承认,我对日志框架不太了解,无法自己想出解决方案。欢迎提供任何帮助和建议。

事实证明,我的问题是由flyway spring测试扩展造成的,该扩展将slf4j简单绑定作为依赖项。即使我自己添加了log4j绑定,slf4j简单绑定仍然被使用,因此我的log4j配置对使用slf4j进行日志记录的代码部分没有影响

更为复杂的是,jOOQ在加载时检查日志框架,如果发现了,则更喜欢slf4j——因此,flyway spring测试扩展增加了slf4j依赖项,迫使jOOQ自动切换日志框架,这让我更加困惑

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

  • 从flyway spring测试依赖项中排除org.slf4j:slf4j api
  • 从flyway spring测试依赖项中排除org.slf4j:slf4j simple
  • 将更新版本的org.slf4j:slf4j api添加到项目中
  • 将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测试组件时引入的。我会修改我的答案,以反映that@AxelFontaine:有趣,你是怎么做的?仅限反射?@LukasEder此处提供的所有信息: