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。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包無效可使用如下方式排除:

org。springframework。boot spring-boot-starter org。springframework。boot spring-boot-starter-logging

如果是其他依賴庫的jar包無效則可透過同樣的方式排除:

com。github。secbr fastdfs-client-plus 1。1。1-RELEASE org。slf4j slf4j-log4j12

然後再重新啟動springboot專案,專案便可正常啟動。