启动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.Log4jLoggerFactory loaded from file:~/.m2/repository/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-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.Log4jLoggerFactory
    at org.springframework.util.Assert.instanceCheckFailed(Assert.java:696)
    at org.springframework.util.Assert.isInstanceOf(Assert.java:596)

很明显项目中并没有用到所谓的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] +- org.springframework.boot:spring-boot-starter:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.3.RELEASE:compile
[INFO] |  |  +- ch.qos.logback:logback-classic:jar:1.1.5:compile
[INFO] |  |  |  \- ch.qos.logback:logback-core:jar:1.1.5:compile
[INFO] |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.5:compile
[INFO] |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.5:compile
[INFO] |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.5:compile
[INFO] |  +- org.springframework:spring-core:jar:4.2.5.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.16:compile
...
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.5:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.5: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项目,项目便可正常启动。

精品SpringBoot 2.x视频教程

《Spring Boot 2.x 视频教程全家桶》,精品Spring Boot 2.x视频教程,打造一套最全的Spring Boot 2.x视频教程。



Spring Boot启动slf4j提示找不到weblogic.xml日志异常插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.choupangxia.com/2020/08/03/spring-boot-slf4j-weblogic-xml/