当前位置:   article > 正文

Spring Boot动态修改日志级别_springboot2.x logging.level 动态刷新

springboot2.x logging.level 动态刷新

一 点睛

1 loggers端点

该端点将为我们提供动态修改Spring Boot应用日志级别的强大功能。该功能的使用非常简单,它依然延续了Spring Boot自动化配置的实现,所以只需要在引入了spring-boot-starter-actuator依赖的条件下就会自动开启该端点的功能。

二 实战

1 引入依赖包

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-actuator</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-test</artifactId>
  13. <scope>test</scope>
  14. </dependency>
  15. </dependencies>

2 配置application.properties

关闭安全认证校验

management.security.enabled=false

3 启动类

  1. package com.didispace;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @SpringBootApplication
  11. public class DemoApplication {
  12. private Logger logger = LoggerFactory.getLogger(getClass());
  13. @RequestMapping(value = "/test", method = RequestMethod.GET)
  14. public String testLogLevel() {
  15. logger.debug("Logger Level :DEBUG");
  16. logger.info("Logger Level :INFO");
  17. logger.error("Logger Level :ERROR");
  18. return "";
  19. }
  20. public static void main(String[] args) {
  21. SpringApplication.run(DemoApplication.class, args);
  22. }
  23. }

三 测试

1 启动应用程序

2 浏览器输入:http://localhost:8080/test

3 控制台输出——由于默认的日志级别为INFO,所以并没有输出DEBUG级别的内容。

  1. 2018-11-03 15:13:50.655 INFO 59148 --- [nio-8080-exec-1] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :INFO
  2. 2018-11-03 15:13:50.655 ERROR 59148 --- [nio-8080-exec-1] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :ERROR

postman发送如下消息配置DEBUG

发送POST请求到/loggers/com.didispace端点

  1. {
  2. "configuredLevel": "DEBUG"
  3. }

5 浏览器输入:http://localhost:8080/test

6 控制台输出——从日志输出,可知动态修改生效了

  1. 2018-11-03 15:17:46.718 DEBUG 59148 --- [nio-8080-exec-7] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :DEBUG
  2. 2018-11-03 15:17:46.718 INFO 59148 --- [nio-8080-exec-7] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :INFO
  3. 2018-11-03 15:17:46.718 ERROR 59148 --- [nio-8080-exec-7] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :ERROR

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/102051
推荐阅读
相关标签
  

闽ICP备14008679号