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

已在Maven pom.xml引入SLF4J,为何仍报“No SLF4J provider found”错误?

问题描述

运行Spring Java程序时遇到SLF4J双重异常:

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J: Ignoring binding found at [jar:file:/C:/Users/user/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Ignoring binding found at [jar:file:/C:/Users/user/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.19.0/log4j-slf4j-impl-2.19.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]

当前pom.xml配置的日志依赖:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.5</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.19.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.19.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.19.0</version>
</dependency>

已尝试调整依赖、排除冲突,但异常未解决。


解决思路

1. 修正Log4J与SLF4J 2.x的适配依赖

SLF4J 2.x改用SPI机制替代1.x的StaticLoggerBinder,而**log4j-slf4j-impl是针对SLF4J 1.x的绑定**,即使版本是2.19.0也会被SLF4J 2.x忽略。需要替换为适配SLF4J 2.x的专用绑定:

<!-- 替换原log4j-slf4j-impl -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j2-impl</artifactId>
    <version>2.19.0</version>
</dependency>

2. 彻底清除旧版SLF4J绑定

错误中出现的logback-classic 1.2.11是间接依赖引入的,用Maven命令定位来源:

mvn dependency:tree -Dincludes=ch.qos.logback:logback-classic

找到引入它的依赖后,添加排除规则:

<dependency>
    <!-- 这里是引入logback的父依赖,例如spring-boot-starter-web -->
    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 锁定SLF4J版本避免冲突

在pom.xml的<dependencyManagement>中强制统一slf4j-api版本,防止间接依赖引入旧版:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.5</version>
        </dependency>
    </dependencies>
</dependencyManagement>

4. 清理Maven缓存并重建

本地仓库缓存可能导致依赖异常,执行命令清理后重新构建:

mvn clean install -U

5. 验证SPI文件完整性

SLF4J 2.x通过META-INF/services/org.slf4j.spi.SLF4JServiceProvider文件识别Provider,解压log4j-slf4j2-impl-2.19.0.jar确认该文件存在且内容正确。如果是Spring Boot项目,检查maven-shade-plugin是否配置了ServicesResourceTransformer,避免SPI文件被覆盖。


内容的提问来源于stack exchange,提问作者Ashley Xu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 13:45:24