springboot动态修改系统日志级别

spring1.5.X版本引入的一个新的控制端点:/loggers,该端点将为我们提供动态修改Spring Boot应用日志级别的强大功能。该功能的使用非常简单,它依然延续了Spring Boot自动化配置的实现,所以只需要在引入了spring-boot-starter-actuator依赖的条件下就会自动开启该端点的功能(更多关于spring-boot-starter-actuator
模块的详细介绍可见:《springboot中使用actuator进行监控》一文)。

###配置:

pom依赖:

1
2
3
4
5
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

yml配置:

1
2
3
4
5
6
7
8
9
10
##运行状态 actuator监控
endpoints:
loggers:
enabled: true
sensitive: false
management:
##服务路径
context-path: /manage
##服务端口
port: 8081

然后启动项目,可以看到端口已经映射成功

1
2
3
... o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
... o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/manage/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
... o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/manage/loggers || /manage/loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()

###查询日志级别:

使用GET请求:

1
/manage/loggers/

会返回所有的日志级别:

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
{
"levels":[
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers":{
"ROOT":{
"configuredLevel":"INFO",
"effectiveLevel":"INFO"
},
"com":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.caiyi":{
"configuredLevel":null,
"effectiveLevel":"INFO"
}
...
}
}

###修改日志级别:

使用POST请求:

1
/manage/loggers/{elephant}

{elephant}为前面查询到的目录。
比如我修改com.caiyi下面的日志级别为debug,访问:

1
http:127.0.0.1:8081/manage/loggers/com.caiyi

请求body中参数:

1
2
3
{
"configuredLevel": "debug"
}

然后再调用查询接口就会发现已经改为debug级别了


springboot动态修改系统日志级别
https://www.wekri.com/springboot/springboot动态修改系统日志级别/
Author
Echo
Posted on
September 12, 2017
Licensed under