【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

posted @ 2025-06-16 10:56  一杯半盏  阅读(399)  评论(0)    收藏  举报