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