You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SLF4JLoggerContext无法转换为LoggerContext问题求助(Maven+Spring Boot)

解决Spring Boot中org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext异常

不用移除spring-boot-starter-web就能搞定这个问题!我之前在Maven构建的Spring Boot项目里也踩过一模一样的坑,核心原因是spring-boot-starter-web默认引入了log4j-to-slf4j这个日志桥接依赖——它会把Log4j的日志请求转发给SLF4J,但如果你的项目直接用到了Log4j Core的API(比如要获取LoggerContext做自定义配置),就会触发这个类型转换冲突。

具体解决步骤:

在Maven的pom.xml中,给spring-boot-starter-web添加依赖排除,把惹麻烦的log4j-to-slf4j从自动引入的列表中剔除:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 排除引发类型转换异常的log4j-to-slf4j依赖 -->
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

额外检查点:

如果你的项目里还有其他Spring Boot Starter(比如spring-boot-starter)也引入了log4j-to-slf4j,同样需要给对应的依赖加上上述排除配置。

验证排除是否成功:

执行以下Maven命令查看依赖树,确认log4j-to-slf4j已经不在类路径中:

mvn dependency:tree | grep log4j-to-slf4j

如果没有任何输出,说明排除成功,此时再运行项目就不会出现那个烦人的类型转换异常了。

另外,确保你的项目已经正确引入了Log4j Core和API依赖(如果之前没配置的话):

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j.version}</version> <!-- 替换为你实际使用的版本 -->
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j.version}</version>
</dependency>

内容的提问来源于stack exchange,提问作者Olga Pshenichnikova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:18:25