一 点睛
1 loggers端点
该端点将为我们提供动态修改Spring Boot应用日志级别的强大功能。该功能的使用非常简单,它依然延续了Spring Boot自动化配置的实现,所以只需要在引入了spring-boot-starter-actuator依赖的条件下就会自动开启该端点的功能。
二 实战
1 引入依赖包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2 配置application.properties
关闭安全认证校验
management.security.enabled=false
3 启动类
package com.didispace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
private Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String testLogLevel() {
logger.debug("Logger Level :DEBUG");
logger.info("Logger Level :INFO");
logger.error("Logger Level :ERROR");
return "";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三 测试
1 启动应用程序
2 浏览器输入:http://localhost:8080/test
3 控制台输出——由于默认的日志级别为INFO,所以并没有输出DEBUG级别的内容。
2018-11-03 15:13:50.655 INFO 59148 --- [nio-8080-exec-1] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :INFO
2018-11-03 15:13:50.655 ERROR 59148 --- [nio-8080-exec-1] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :ERROR
4 postman发送如下消息配置DEBUG
发送POST请求到/loggers/com.didispace端点
{
"configuredLevel": "DEBUG"
}
5 浏览器输入:http://localhost:8080/test
6 控制台输出——从日志输出,可知动态修改生效了
2018-11-03 15:17:46.718 DEBUG 59148 --- [nio-8080-exec-7] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :DEBUG
2018-11-03 15:17:46.718 INFO 59148 --- [nio-8080-exec-7] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :INFO
2018-11-03 15:17:46.718 ERROR 59148 --- [nio-8080-exec-7] ication$$EnhancerBySpringCGLIB$$302a5f35 : Logger Level :ERROR