springboot1.5之前的版本好像没有这个特性需要自己实现一个actuator endpoint,1.5之后的actuator新增了endpoint支持查看日志文件,查看日志级别和动态改变日志级别.
默认使用的是logback作为日志框架,需要引入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
测试目的,可以把权限问题关闭,不然访问endpoint的时候会报401错误,在application.yml中配置:
management: context-path: /admin security: enabled: false
然后启动应用程序,在浏览器里面输入http://localhost:8080/admin/loggers就可以看到所有的loggers和相应的日志级别信息,也可以输入指定的logger name查看指定的logger信息:
http://localhost:9090/admin/loggers/com.example.webdemo.controller
此时显示:
{
"configuredLevel": "DEBUG",
"effectiveLevel": "DEBUG"
}
比如我要改变com.example.webdemo.controller的日志级别,可以发送一个post请求:
curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "WARN"}' http://localhost:8080/loggers/com.example.webdemo.controller
再在浏览器里面查看一下http://localhost:9090/admin/loggers/com.example.webdemo.controller
此时日志级别已经改变:
{
"configuredLevel": "WARN",
"effectiveLevel": "WARN"
}
这样调试日志变得跟方便了,再做一个web页面来管理这些logger配置使用起来更方便