Spring Boot 2动态修改日志级别
本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本。
作为程序猿,定位问题是我们的日常工作,而日志是我们定位问题非常重要的依据。传统方式定位问题时,往往是如下步骤:
- 将日志级别设低,例如
DEBUG
; - 重启应用;
- 复现问题,观察日志;
如果能动态修改日志级别(无需重启应用,就能立刻刷新),那绝对 如猫添翼
。事实上,从 Spring Boot 1.5
开始,Spring Boot Actuator
组件就已提供动态修改日志级别的能力。
TIPS
- 其实更低版本也只需简单扩展,即可实现动态修改日志级别。
- 对Spring Boot Actuator感到陌生的童鞋,可先前往 Spring Boot Actuator 了解基础用法。
废话不多说了,亮代码吧。
编码
加依赖
1
2
3
4
5
6
7
8<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>这里的
spring-boot-starter-web
不是必须的,只是下面测试代码要用到。写代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22package com.itmuch.logging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author itmuch.com
*/
public class TestController {
private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
public String simple() {
LOGGER.debug("这是一个debug日志...");
return "test";
}
}写配置:
1
2
3
4
5management:
endpoints:
web:
exposure:
include: 'loggers'由于Spring Boot 2.x默认只暴露
/health
以及/info
端点,而日志控制需要用到/loggers
端点,故而需要设置将其暴露。
代码编写完成啦。
测试
/loggers
端点提供了 查看
以及 修改
日志级别的能力。
测试1:查看当前应用各包/类的日志级别
访问 http://localhost:8080/actuator/loggers
,可看到类似如下的结果:
1 | { |
测试2:查看指定包/类日志详情
访问 http://localhost:8080/actuator/loggers/com.itmuch.logging.TestController
,可看到类似如下的结果:
1 | {"configuredLevel":null,"effectiveLevel":"INFO"} |
由测试不难发现,想看哪个包/类的日志,只需构造 /actuator/loggers/包名类名全路径
去访问即可。
测试3:修改日志级别
在 TestController
类中,笔者编写设置了一条日志 LOGGER.debug("这是一个debug日志...");
,而由测试1,默认的日志级别是INFO,所以不会打印。下面来尝试将该类的日志级别设为DEBUG。
1 | curl -X POST http://localhost:8080/actuator/loggers/com.itmuch.logging.TestController \ |
如上,只需发送一个POST请求,并将请求body设为:{"configuredLevel":"debug"}
即可。
此时,访问 localhost:8080/test
会看到类似如下的日志:
1 | 2019-03-28 16:24:04.513 DEBUG 19635 --- [nio-8080-exec-7] com.itmuch.logging.TestController : 这是一个debug日志... |
并且,此时再访问 http://localhost:8080/actuator/loggers/com.itmuch.logging.TestController
,可看到类似如下的结果:
1 | {"configuredLevel":"DEBUG","effectiveLevel":"DEBUG"} |
说明已成功动态修改日志级别。
原理分析
TIPS
本节着重分析如何实现动态修改。
Actuator有约定, /actuator/xxx
端点的定义代码在 xxxEndpoint
中。故而,找到类 org.springframework.boot.actuate.logging.LoggersEndpoint
,可看到类似如下的代码:
1 |
|
其中, Endpoint
、WriteOperation
、@Selector
都是Spring Boot 2.0开始提供的新注解。
@Endpoint(id = "loggers")
用来描述Spring Boot Actuator
的端点,这样就会产生一个/actuator/loggers
的路径,它类似于Spring MVC的 @RequestMapping("loggers")
。
@WriteOperation
表示这是一个写操作,它类似于Spring MVC的 @PostMapping
。Spring Boot Actuator还提供了其他操作,如下表:
Operation | HTTP method |
---|---|
@ReadOperation |
GET |
@WriteOperation |
POST |
@DeleteOperation |
DELETE |
@Selector
用于筛选 @Endpoint
注解返回值的子集,它类似于Spring MVC的 @PathVariable
。
这样,上面的代码就很好理解了—— configureLogLevel
方法里面就一行代码 :this.loggingSystem.setLogLevel(name, configuredLevel);
,发送POST请求后,name就是我们传的包名或者类名,configuredLevel就是我们传的消息体。
怎么实现动态修改的呢?不妨点进去看看,然后发现代码如下:
1 | // org.springframework.boot.logging.LoggingSystem#setLogLevel |
嘿嘿,没事,肯定有实现类, 该方法在如下实现类被实现:
1 | # 适用于java.util.logging的LoggingSystem |
Spring Boot 2.x中,默认使用Logback,因此进入到 LogbackLoggingSystem
中,代码如下:
1 |
|
至此,就真相大白了。其实根本没有黑科技,Spring Boot本质上还是使用了Logback的API,ch.qos.logback.classic.Logger.setLevel
实现日志级别的修改。
你可能会好奇
你可能会好奇,LoggingSystem有这么多实现类,Spring Boot怎么知道什么情况下用什么LoggingSystem呢?可在 org.springframework.boot.logging.LoggingSystem
找到类似如下代码:
1 | public abstract class LoggingSystem { |
由代码不难发现,其实就是构建了一个名为 SYSTEMS
的map,作为各种日志系统的字典;然后在 get
方法中,看应用是否加载了map中的类;如果加载了,就通过反射,初始化响应 LoggingSystem
。例如:Spring Boot发现当前应用加载了 ch.qos.logback.core.Appender
,就去实例化 org.springframework.boot.logging.logback.LogbackLoggingSystem
。
界面
本文是使用 curl
手动发送 POST
请求手动修改日志级别的,该方式不适用生产,因为很麻烦,容易出错。生产环境,建议根据Actuator提供的RESTful API定制界面,或使用 Spring Boot Admin
,可视化修改日志级别,如下图所示:
想修改哪个包/类的日志级别,直接点击即可。
配套代码
GitHub:https://github.com/eacdy/spring-boot-study/tree/master/spring-boot-logging-change-logging-level
Gitee:https://gitee.com/itmuch/spring-boot-study/tree/master/spring-boot-logging-change-logging-level
评论系统未开启,无法评论!