springboot动态调整日志级别
作者:互联网
前言:我们在调试程序时,查看日志是主要途径之一。当项目部署到开发测试环境之后,就无法随心所欲通过修改配置文件来修改日志级别,此时就需要动态的调整日志级别来满足调试需求。
1.引入actuator依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.修改application.properties
#暴露loggers这个endpoint
management.endpoints.web.exposure.include=health,info,loggers
3.查看日志信息
- 访问http://ip:port/actuator/loggers 可以看到当前项目每个包的日志级别,可以看到springboot的默认日志级别为info,也就是ROOT对应的日志级别
{
"levels": [
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers": {
"ROOT": {
"configuredLevel": "INFO",
"effectiveLevel": "INFO"
},
"com": {
"configuredLevel": null,
"effectiveLevel": "INFO"
},
"com.yao": {
"configuredLevel": null,
"effectiveLevel": "INFO"
},
"com.yao.logdemo": {
"configuredLevel": null,
"effectiveLevel": "INFO"
},
...
}
4.修改日志级别
- 修改整个项目日志级别为error
发送post请求到 http://ip:port/actuator/loggers/ROOT ,请求body为
{
"configuredLevel":"ERROR"
}
再次查看日志信息,可以看到所有ROOT以及所有包的日志级别都变成ERROR了
{
"levels": [
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers": {
"ROOT": {
"configuredLevel": "ERROR",
"effectiveLevel": "ERROR"
},
"com": {
"configuredLevel": null,
"effectiveLevel": "ERROR"
},
"com.yao": {
"configuredLevel": null,
"effectiveLevel": "ERROR"
},
"com.yao.logdemo": {
"configuredLevel": null,
"effectiveLevel": "ERROR"
},
...
}
- 修改com.yao包日志级别为debug
发送post请求到 http://ip:port/actuator/loggers/com.yao ,请求body为
{
"configuredLevel":"DEBUG"
}
再次查看日志信息,可以看到com.yao包及包含的包日志级别都变为DEBUG了
{
"levels": [
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers": {
"ROOT": {
"configuredLevel": "ERROR",
"effectiveLevel": "ERROR"
},
"com": {
"configuredLevel": null,
"effectiveLevel": "ERROR"
},
"com.yao": {
"configuredLevel": "DEBUG",
"effectiveLevel": "DEBUG"
},
"com.yao.logdemo": {
"configuredLevel": null,
"effectiveLevel": "DEBUG"
},
...
}
标签:springboot,com,configuredLevel,yao,ERROR,effectiveLevel,日志,级别 来源: https://www.cnblogs.com/henryyao/p/10488213.html