一、SpringBoot 中日志的设计

  SpringBoot 框架在企业中使用的越来越普遍,SpringBoot 日志也是开发中常用的日志系统。SpringBoot 默认就是使用 SLF4J 作为日志门面,Logback 作为日志实现来记录日志。

1、依赖

  spring-boot-starter-logging 是 SpringBoot 日志的依赖。

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

2、依赖关系图

image.png

3、总结

  1. SpringBoot 底层默认使用 logback 作为日志实现。
  2. 使用了 slf4j 作为日志门面。
  3. 将 JUL 也转换成 slf4j
  4. 也可以使用 log4j2 作为日志门面,但是最终也是通过 slf4j 调用 logback

二、SpringBoot 中日志的使用

1、导入依赖

  这里我们导入 spring-boot-starter-web 依赖,它包含 spring-boot-starter-logging 依赖。spring-boot-starter-test 依赖用于单元测试。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2、快速入门

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class Tests {

    // 日志记录器
    public static final Logger LOGGER = LoggerFactory.getLogger(Tests.class);

    @Test
    public void test() {
        // 日志记录输出
        LOGGER.error("error");
        LOGGER.warn("warn");
        LOGGER.info("info");
        LOGGER.debug("debug");
        LOGGER.trace("trace");
    }
}

三、SpringBoot 中日志的配置

  在实际开发中,我们往往需要根据需求更改日志的配置信息,更改日志配置信息的方式如下:

1、在 SpringBoot 配置文件中配置

  在 SpringBoot 的配置文件中修改日志的配置,这里使用 yml 的方式。

# 指定自定义 Logger 对象日志级别
logging:
  level:
    top:
      # 指定日志的打印级别
      zyxwmj: trace
  file:
    # 指定日志文件存放的目录,默认的文件名为 spring.log
    path: E:\
  pattern:
    # 指定日志的打印格式
    console:  "[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c %M %L ----- %m%n"
    file:  '[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c %M %L +++++ %m%n'

2、单独配置

  给日志框架配置自己的配置文件,SpringBoot 就不使用默认配置了。

日志框架配置文件
Logbacklogback-spring.xml(推荐)、logback.xml
log4j2log4j2-spring.xml(推荐) 、log4j2.xml
JULlogging.properties

3、SpringBoot 解析日志配置

  在项目中,我们生产环境和开发环境的日志可能不同,通过 springProfile 标签可以根据项目的配置更改日志的配置,这里我们以 Logback 框架为例。
  注意:只有以logback-spring.xml命名的配置文件可以使用springProfile 标签,以logback.xml命名的配置文件不能使用。

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!--控制输出流对象 默认为 System.out-->
        <target>System.err</target>
        <!--日志消息格式配置-->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <springProfile name="dev">
                <pattern>[%-5level]+++ %d{yyyy-MM-dd HH:mm:ss} %c %M %L %m%n"</pattern>
            </springProfile>
            <springProfile name="pro">
                <pattern>[%-5level]--- %d{yyyy-MM-dd HH:mm:ss} %c %M %L %m%n"</pattern>
            </springProfile>
        </encoder>
    </appender>

  在 SpringBoot 配置文件中配置当前项目使用的配置。

# 指定项目使用的具体环境
spring:
  profiles:
    active: dev

四、SpringBoot 整合 SLF4J + Log4j2

  首先将 spring-boot-starter-web 依赖中的 spring-boot-starter-logging 依赖排除,然后导入 spring-boot-starter-log4j2 依赖即可。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除 logback-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加log4j2-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

标题:SpringBoot 中日志的使用
作者:Yi-Xing
地址:http://zyxwmj.top/articles/2020/03/25/1585106180546.html
博客中若有不恰当的地方,请您一定要告诉我。前路崎岖,望我们可以互相帮助,并肩前行!