从Spring Boot 1.5开始,新的loggers
器执行器端点允许在运行时查看和更改应用程序记录级别。
- 将
spring-boot-actuator
添加到您的项目
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 禁用
loggers
或所有端点的安全性
将management.security.enabled
设置为false
或将endpoints.loggers.sensitive
为false
可禁用安全性。 请注意,后者仅更改loggers
端点。
- 获取所有记录器的详细信息
在浏览器中或使用curl
执行/loggers
端点。 您将获得记录器配置的详细视图,例如(片段):
{"levels": ["OFF","ERROR","WARN","INFO","DEBUG","TRACE"],"loggers": {"ROOT": {"configuredLevel": "TRACE","effectiveLevel": "TRACE"},"org": {"configuredLevel": null,"effectiveLevel": "TRACE"},"pl.codeleak.demos.sbt": {"configuredLevel": null,"effectiveLevel": "TRACE"},"pl.codeleak.demos.sbt.Application": {"configuredLevel": null,"effectiveLevel": "TRACE"}}
}
- 获取选定的记录器详细信息
使用以下端点来获取所选记录器的详细信息:
/logger/{logger}
例子:
λ curl -i http://localhost:8080/loggers/ROOT{
"configuredLevel": null,
"effectiveLevel": "TRACE"
}λ curl -i http://localhost:8080/loggers/pl.codeleak.demos{
"configuredLevel": null,
"effectiveLevel": "TRACE"
}
- 在运行时更新选定的记录器级别
执行POST
请求:
curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "TRACE"}' http://localhost:8080/loggers/ROOT
源代码
可以在https://github.com/kolorobot/spring-boot-thymeleaf存储库中找到示例配置。
翻译自: https://www.javacodegeeks.com/2017/03/spring-boot-configure-log-level-runtime-using-actuator-endpoint.html