在Spring Boot项目中,如果你已经配置了logback-spring.xml文件但发现它没有被使用,而程序内部的日志输出显示使用的是slf4j的默认输出格式(例如,你看到了org.slf4j.Logger@Slf4j注解),这可能是因为Spring Boot默认的自动配置机制导致的。

Spring Boot默认会寻找并使用以下日志框架之一:

  1. Logback (优先级最高)
  2. Log4j2
  3. JCL (Jakarta Commons Logging)
  4. JUL (Java Util Logging)

如果logback-spring.xml没有被使用,可能的原因有以下几点:

1. 文件名或位置不正确

确保你的logback-spring.xml文件位于src/main/resources目录下,并且文件名完全匹配。Spring Boot会特别寻找logback-spring.xml来覆盖默认的Logback配置。

2. 依赖冲突

检查你的项目依赖中是否同时包含了多个日志框架。例如,如果项目中不小心引入了Log4j的依赖,Spring Boot可能会选择使用Log4j而不是Logback。检查pom.xmlbuild.gradle文件,确保只包含一个日志框架的依赖。

3. Spring Boot版本问题

不同版本的Spring Boot处理日志配置的方式可能有所不同。确认你使用的Spring Boot版本是否支持logback-spring.xml配置。

4. 明确指定日志框架

application.propertiesapplication.yml文件中,你可以明确指定日志框架为Logback:Properties

logging.framework=ch.qos.logback
  • 1.

但是,通常情况下,如果项目中添加了Logback的依赖,Spring Boot会自动选择使用Logback。

解决方法
  • 确保logback-spring.xml文件存在于正确的路径。
  • 检查并清理项目依赖,确保没有引入其他日志框架的冲突。
  • 更新Spring Boot版本,确保其支持你想要使用的日志配置方式。
  • 如果上述方法都无法解决问题,尝试重启IDE和Maven/Gradle构建工具,确保所有更改都已应用。

最后,如果仍然遇到问题,可以尝试在控制台输出日志配置信息,以帮助诊断问题所在:Java

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;

public class LogConfigChecker {
    public static void main(String[] args) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        System.out.println("Current log configuration: " + lc);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

这将输出当前的日志配置详情,帮助你确定问题所在。

Logback 是一个基于 Java 的日志框架,它是 Log4j 的一个流行替代品,提供了更好的性能和一些额外的功能。Logback 由三个模块组成:logback-corelogback-classiclogback-access

  1. logback-core:这是 Logback 的核心模块,包含了 Logback 必需的基本类和组件。
  2. logback-classic:这个模块实现了 SLF4J API,并提供了与 Log4j 兼容的 API。logback-classic 依赖于 logback-core
  3. logback-access:这个模块提供了与 Servlet 容器集成的能力,可以捕获 HTTP 访问日志。logback-access 同样依赖于 logback-core
如何在项目中添加 Logback 的 jar 包

在 Maven 项目中,你可以在 pom.xml 文件中添加以下依赖:Xml

<dependencies>
    <!-- logback-classic 包含了对 SLF4J 的实现 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.10</version> <!-- 根据实际情况更新到最新版本 -->
    </dependency>

    <!-- 如果你需要 logback-access 的功能 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-access</artifactId>
        <version>1.2.10</version>
    </dependency>
</dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

你可以在logback-spring.xml配置文件中添加以下:Xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
    <springProperty scop="context" name="spring.application.name" source="spring.application.name" defaultValue=""/>
    <property name="log.path" value="logs/${spring.application.name}"/>

    <!-- 彩色日志格式 -->
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} [%L] %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <!-- 彩色日志依赖的渲染类 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

    <!-- Console log output -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <!-- Log file debug output -->
    <appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/debug.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <maxFileSize>50MB</maxFileSize>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Log file error output -->
    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <maxFileSize>50MB</maxFileSize>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
    </appender>

    <!-- 异步写日志 -->
    <appender name="async_file" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>1024</queueSize>
        <appender-ref ref="debug"/>
    </appender>
    <appender name="async_error_file" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>1024</queueSize>
        <appender-ref ref="error"/>
    </appender>

    <!-- Level: FATAL 0  ERROR 3  WARN 4  INFO 6  DEBUG 7 -->
    <root level="INFO">
        <appender-ref ref="console"/>
        <appender-ref ref="async_file"/>
        <appender-ref ref="async_error_file"/>
    </root>
</configuration>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.

对于 Gradle 项目,你可以在 build.gradle 文件中添加如下依赖:Groovy

dependencies {
    implementation 'ch.qos.logback:logback-classic:1.2.10'
    // 如果需要 logback-access 的功能
    implementation 'ch.qos.logback:logback-access:1.2.10'
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
注意事项
  • 当你添加了 logback-classic 或 logback-access 依赖后,Maven 或 Gradle 会自动解决它们所依赖的 logback-core 依赖。
  • 版本号应根据实际需要进行调整,建议查阅 Maven 中央仓库或相关文档获取最新的稳定版本。
  • 在 Spring Boot 项目中,通常不需要显式添加 Logback 的依赖,因为 Spring Boot 默认就包含了 Logback,并通过 spring-boot-starter-logging 来管理日志框架的依赖。

以上就是在 Java 项目中引入 Logback 日志框架的方法。