Spring Boot启动slf4j提示找不到weblogic.xml日志异常
启动Spring Boot项目时,会遇到如下关于slf4j相关的日志异常情况,导致项目无法启动。
相关异常信息如下:
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.JDK14LoggerFactory loaded from file:/D:/Program%20Files/apache-maven-3.5.4/repository/org/slf4j/slf4j-jdk14/1.7.30/slf4j-jdk14-1.7.30.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.JDK14LoggerFactory
at org.springframework.util.Assert.instanceCheckFailed(Assert.java:702)
at org.springframework.util.Assert.isInstanceOf(Assert.java:602)
很明显项目中并没有用到所谓的weblogic.xml,那么怎么会报关于WEB-INF/weblogic.xml配置的异常呢?
其中关键新增在这里:
LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation......
就是说Logback已经在classpath中存在,但LoggerFactory并不是Logback的相关日志上线文内容。此时就应该意识到有Logback依赖冲突。
此时,可用过查看maven依赖来排查问题,在项目跟目录执行如下命令:
mvn dependency:tree
展示结果内容如下:
[INFO] com.bstek.urule:urule-springboot:jar:0.0.1-SNAPSHOT
[INFO] +- com.bstek.urule:urule-console:jar:2.1.8-SNAPSHOT:compile
[INFO] | +- com.bstek.urule:urule-core:jar:2.1.8-SNAPSHOT:compile
[INFO] | | +- org.antlr:antlr4-runtime:jar:4.5:compile
[INFO] | | | \- org.abego.treelayout:org.abego.treelayout.core:jar:1.0.1:compile
[INFO] | | +- org.slf4j:slf4j-jdk14:jar:1.7.30:compile
[INFO] | | +- commons-lang:commons-lang:jar:2.6:compile
[INFO] | | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | | | \- xml-apis:xml-apis:jar:1.0.b2:compile
......
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.3.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.3.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.3.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.3.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.3.5.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
很显然,可以看出多处引用了同样的slf4j-api的jar包。也就是jar包冲突了,此时将依赖的jar包排除掉,只有一处即可。
如果是springboot中的jar包无效可使用如下方式排除:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
如果是其他依赖库的jar包无效则可通过同样的方式排除:
<dependency>
<groupId>com.github.secbr</groupId>
<artifactId>fastdfs-client-plus</artifactId>
<version>1.1.1-RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
然后再重新启动springboot项目,项目便可正常启动。

浙公网安备 33010602011771号