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

Spring Boot 3.1.3启动遇SLF4J无提供者错误,日志未生成求助

解决Spring Boot 3.1.3中SLF4J无提供者及日志文件不生成问题

核心问题分析

你排除了Spring Boot默认的spring-boot-starter-logging(默认绑定Logback),但手动引入的Log4j2依赖缺少SLF4J与Log4j2的绑定桥接依赖,同时单独引入log4j-api、log4j-core无法和Spring Boot的依赖管理体系兼容,导致SLF4J找不到日志提供者,进而无法生成日志文件。

解决方案步骤

1. 替换为Spring Boot官方Log4j2 Starter

删除手动添加的log4j-api、log4j-core以及尝试引入的log4j-slf4j-impl,改用Spring Boot提供的spring-boot-starter-log4j2——它会自动包含所有必要的依赖(包括SLF4J绑定、Log4j核心库),并由Spring Boot统一管理版本,避免冲突:

<!-- 删除以下手动添加的Log4j依赖 -->
<!-- <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
</dependency> -->

<!-- 添加Spring Boot Log4j2 Starter -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

2. 统一排除默认日志依赖

除了spring-boot-starter,spring-boot-starter-web也会间接引入spring-boot-starter-logging,需要一并排除:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 排查依赖冲突

用Maven命令查看依赖树,确认没有其他依赖引入SLF4J的其他绑定(如Logback、SLF4J Simple):

mvn dependency:tree | grep slf4j

如果发现冲突绑定,通过<exclusion>标签从对应依赖中排除。

4. 添加Log4j2配置文件

在src/main/resources下创建log4j2.xml(或log4j2.properties),配置日志输出规则。示例配置如下(会生成滚动日志文件):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <!-- 控制台输出配置 -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <!-- 文件滚动输出配置 -->
        <RollingFile name="RollingFile" fileName="logs/app.log"
                     filePattern="logs/app-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

验证结果

重新构建并启动应用,此时SLF4J会通过starter自动引入的log4j-slf4j-impl绑定到Log4j2,CLI不会再出现SLF4J无提供者错误,同时日志文件会在项目根目录的logs文件夹下生成。

内容的提问来源于stack exchange,提问作者karthikeyan S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 23:12:34