解决报错:Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND

痛定思痛。 2023-07-17 09:57 8阅读 0赞

错误现象

Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html\#log4jDelegationLoop for more details.
at org.slf4j.impl.Log4jLoggerFactory.(Log4jLoggerFactory.java:54)
… 24 more

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYxODMx_size_16_color_FFFFFF_t_70

原因

  1. SLF4JAPI被设计成一次只绑定一个底层日志框架。如果在类路径上存在不止一个绑定,则SLF4J将发出警告,列出这些绑定的位置。
  2. SLF4J在这个警告中提供的位置列表通常提供足够的信息来标识在不必要的SLF4J绑定中向项目中过渡的依赖性。在项目的POM.XML文件中,在声明不法依赖时排除此SLF4J绑定。

解决方法

以我为例,我是在pom中引入 fastdfs-client-java 依赖时出现这个错误,所以只要在该依赖中屏蔽slf4j-log4j12依赖即可:

  1. <!--FastDFS-Client-Java-->
  2. <dependency>
  3. <groupId>org.csource</groupId>
  4. <artifactId>fastdfs-client-java</artifactId>
  5. <version>1.27-SNAPSHOT</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.slf4j</groupId>
  9. <artifactId>slf4j-log4j12</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>

官方示例

cassandra-all版本0.8-1声明两者Log4Jslf4j-log4j12作为编译时依赖项。因此,当你包括cassandra-all作为项目中的依赖项,cassandra-all声明将导致两者slf4j-log4j12.jarlog4j.jar作为依赖而被拉进去。如果您不希望使用Log4J作为SLF4J后端,则可以指示Maven排除这两个工件,如下所示:

  1. <dependencies>
  2. <dependency>
  3. <groupId> org.apache.cassandra</groupId>
  4. <artifactId>cassandra-all</artifactId>
  5. <version>0.8.1</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.slf4j</groupId>
  9. <artifactId>slf4j-log4j12</artifactId>
  10. </exclusion>
  11. <exclusion>
  12. <groupId>log4j</groupId>
  13. <artifactId>log4j</artifactId>
  14. </exclusion>
  15. </exclusions>
  16. </dependency>
  17. </dependencies>

发表评论

表情:
评论列表 (有 0 条评论,8人围观)

还没有评论,来说两句吧...

相关阅读