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

依赖树含slf4j-api但SLF4J注解日志仍无法正常工作

问题

我正在开发一个Spring Boot小型项目,日志功能无法正常工作,收到以下提示信息:

2025-03-02 13:44:41 [JDALogger] [WARN] Using fallback logger due to missing SLF4J implementation.
2025-03-02 13:44:41 [JDALogger] [WARN] Please setup a logging framework to use JDA.
2025-03-02 13:44:41 [JDALogger] [WARN] You can use our logging setup guide https://jda.wiki/setup/logging/
2025-03-02 13:44:41 [JDALogger] [WARN] To disable the fallback logger, add the slf4j-nop dependency or use JDALogger.setFallbackLoggerEnabled(false)
SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.

我的pom.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.11.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>5.14.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.pitest</groupId>
            <artifactId>pitest-parent</artifactId>
            <version>1.16.2</version>
            <type>pom</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.43.0</version>

                <configuration>
                    <java>
                        <palantirJavaFormat/>
                    </java>
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.pitest</groupId>
                <artifactId>pitest-maven</artifactId>
                <version>1.16.2</version>
            </plugin>
        </plugins>
    </build>
</project>

我使用了SLF4J注解,且在依赖树中能看到slf4j-api库,但日志仍无法正常工作,手动添加相关依赖后才生效。请问这是为什么?slf4j-api不包含日志实现吗?

解答

slf4j-api确实不包含日志实现,它只是一套日志抽象规范,仅定义了日志的接口和调用标准,本身不负责实际的日志输出、持久化等具体操作。要让日志功能正常运行,必须搭配具体的日志实现框架(比如Logback、Log4j2等)。

你的依赖树中出现的slf4j-api是JDA依赖传递引入的,但只有抽象接口没有具体实现,所以SLF4J找不到可用的日志提供者,才会触发"No SLF4J providers were found"的警告,最终默认使用无操作(NOP)日志或者JDA的降级 fallback logger,导致日志无法正常输出。

针对你的Spring Boot项目,有两种简单的解决方式:

  1. 直接引入Spring Boot官方的日志启动器,它已经整合了slf4j-api和Logback(SLF4J的原生实现),无需手动管理版本:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>
  1. 单独添加Logback的依赖:
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.5.6</version>
</dependency>

添加完成后,SLF4J就能找到对应的日志实现,日志功能即可正常工作。

内容的提问来源于stack exchange,提问作者Bejenariu Ionut

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 01:38:10