Spring Boot 通过 Actuator 动态修改日志级别

Spring Boot 通过 Actuator 动态修改日志级别

环境

JDK 1.8 +

Spring Boot 1.5+

依赖

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

实例

application.properties

management.security.enabled=false

DemoApplication.java

@SpringBootApplication
@RestController
public class DemoApplication {

  private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @GetMapping("/greeting")
  public String greeting() {
    logger.debug("-----> debug level ");
    logger.info("-----> info level ");
    logger.warn("-----> warn level ");
    logger.error("-----> error level ");
    return "Hello Greeting";
  }
}

访问 http://127.0.0.1:8080/greeting, 下面是默认的日志信息。

2019-05-08 16:27:31.705  INFO 3568 --- [nio-8080-exec-4] com.example.demo.DemoApplication         : -----> info level 
2019-05-08 16:27:31.705  WARN 3568 --- [nio-8080-exec-4] com.example.demo.DemoApplication         : -----> warn level 
2019-05-08 16:27:31.705 ERROR 3568 --- [nio-8080-exec-4] com.example.demo.DemoApplication         : -----> error level 

查看日志等级

curl http://127.0.0.1:8080/loggers/com.example.demo
{"configuredLevel":null,"effectiveLevel":"INFO"}

修改日志等级

 curl -X POST "http://127.0.0.1:8080/loggers/com.example.demo" \
>  -H "Content-Type:application/json;charset=utf-8" \
>  -d $'{"configuredLevel":"debug"}'

访问 http://127.0.0.1:8080/greeting

2019-05-08 16:34:23.335 DEBUG 3568 --- [nio-8080-exec-9] com.example.demo.DemoApplication         : -----> debug level 
2019-05-08 16:34:23.335  INFO 3568 --- [nio-8080-exec-9] com.example.demo.DemoApplication         : -----> info level 
2019-05-08 16:34:23.335  WARN 3568 --- [nio-8080-exec-9] com.example.demo.DemoApplication         : -----> warn level 
2019-05-08 16:34:23.335 ERROR 3568 --- [nio-8080-exec-9] com.example.demo.DemoApplication         : -----> error level 
添加新评论