1. spring boot 所使用的日志系统
??spring boot 默认就是使用 SLF4J 作为日志门面,logback 作为日志实现来记录日志。
2. spring boot 中的日志设计
(1) 需要导入的 maven 依赖
??在 spring boot 中,日志依赖已经集成于 web 依赖里面。所以我们只需要导入下面的 maven 依赖即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
(2) 依赖关系图
??spring boot 中的日志依赖(因为 web 依赖里面已经包含了下面的 logging 依赖,所以不需要单独导入了)
<dependency>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
(3) 总结
- spring boot 底层默认使用 logback 作为日志实现
- 使用了 SLF4J 作为日志门面
- 可以将 JUL 转换为 slf4j
- 也可以使用 log4j2 作为日志门面,但是最终也是通过 slf4j 调用 logback
3. spring boot 中测试打印日志
(1) 第一种打印日志的方法
package com.peng.test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class LogTest {
private static final Logger LOGGER = LoggerFactory.getLogger(LogTest.class);
@Test
public void contextLoad() {
LOGGER.error("error");
LOGGER.warn("warn");
LOGGER.info("info");
LOGGER.debug("debug");
LOGGER.trace("trace");
String name = "wang";
Integer age = 18;
LOGGER.info("用户:{},{}", name, age);
try {
int i = 1/0;
} catch (Exception e) {
LOGGER.error("出现异常:", e);
}
}
}
(2) 第二种打印日志的方法
??引入 maven 依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
package com.peng.test;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@Slf4j
public class LogTest {
@Test
public void contextLoad() {
log.error("error");
log.warn("warn");
log.info("info");
log.debug("debug");
log.trace("trace");
String name = "wang";
Integer age = 18;
log.info("用户:{},{}", name, age);
try {
int i = 1/0;
} catch (Exception e) {
log.error("出现异常:", e);
}
}
}
4. 在 spring boot 中配置日志信息
??当 spring boot 使用 logback 作为日志实现时。我们在 resources 目录下创建 logback-spring.xml 来配置日志信息的输出
(1) 控制台日志输出基本配置
??logback-spring.xml 的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m %n"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
</root>
</configuration>
(2) 控制台 + 日志文件输出配置
??logback-spring.xml 的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m %n"/>
<property name="log_dir" value="E:"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}\logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
(3) 控制台、日志文件、html文件输出配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m %n"/>
<property name="log_dir" value="E:"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}\logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
<file>${log_dir}\logback.html</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%c%M%L%thread%m</pattern>
</layout>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
<appender-ref ref="htmlFile"/>
</root>
</configuration>
(4) 日志文件的拆分输出配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m %n"/>
<property name="log_dir" value="E:"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}\roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}\rolling.%d{yyyy-MM-dd-HH-mm-ss}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="rollFile"/>
</root>
</configuration>
(5) 日志过滤器配置
??给每个 appender 对象指定一个过滤器,让每个 appender 输出我们指定的日志级别
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m %n"/>
<property name="log_dir" value="E:"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}\roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}\rolling.%d{yyyy-MM-dd-HH-mm-ss}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="rollFile"/>
</root>
</configuration>
(6) 异步日志的输出
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m %n"/>
<property name="log_dir" value="E:"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}\roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}\rolling.%d{yyyy-MM-dd-HH-mm-ss}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
</appender>
<appender name="async" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="rollFile"/>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="async"/>
</root>
</configuration>
5. spring boot 不同运行环境配置不同日志保存路径
(1) 在 application.yaml 的配置文件中指定不同的运行环境
??开发环境(dev)
spring:
profiles:
active: dev
??生产环境(prod)
spring:
profiles:
active: prod
(2) 配置不同环境的日志文件保存路径的配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m %n"/>
<springProfile name="dev">
<property name="log_dir" value="E:"/>
</springProfile>
<springProfile name="prod">
<property name="log_dir" value="E:\aaa"/>
</springProfile>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
</appender>
<appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log_dir}\roll_logback.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${pattern}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${log_dir}\rolling.%d{yyyy-MM-dd-HH-mm-ss}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="rollFile"/>
</root>
</configuration>
|