【slf4j沖突】LoggerFactory is not a Logback LoggerContext but Logback is on the classpath
問題
Failed to load class "org.slf4j.impl.StaticLoggerBinder",slf4j-api-1.7.30.jar 和某些類沖突
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
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.helpers.NOPLoggerFactory loaded from file:/Users/slankka/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-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.helpers.NOPLoggerFactory
at org.springframework.util.Assert.instanceCheckFailed(Assert.java:592)
at org.springframework.util.Assert.isInstanceOf(Assert.java:511)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:401)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:128)
分析
報錯信息已經很清晰指出了 slf4j-api-1.7.30.jar,通過 maven helper 插件快速檢查 log4j, log4j2沖突,竟然沒有。
進一步分析,發現這會不在以前的工作經驗范圍了,因為升級了 SpringBoot版本。
猜測新版的 slf4j 日志系統對舊的版本有要求,結果發現是 Velocity 攜帶了 slf4j-api: 1.7.x
解決
排除 slf4j-api 1.x 的版本即可
結論
SpringBoot 3.x 內置 Log4j 2.x 不再兼容 Log4j 1.x,需要排除否則沖突
參考鏈接
spring.io | SpringBoot Logging
詳細內容參見
Github Gist

浙公網安備 33010602011771號