Spring Boot教程(九)Spring Boot 1.5.x新特性:动态修改日志级别

loggers端点

本文咱们就来看看Spring Boot 1.5.x中引入的一个新的控制端点:/loggers,该端点将为咱们提供动态修改Spring Boot应用日志级别的强大功能。该功能的使用很是简单,它依然延续了Spring Boot自动化配置的实现,因此只须要在引入了spring-boot-starter-actuator依赖的条件下就会自动开启该端点的功能(更多关于spring-boot-starter-actuator模块的详细介绍可见:《Spring Boot Actuator监控端点小结》一文)。html

下面,咱们不妨经过一个实际示例来看看如何使用该功能:web

  • 构建一个基础的Spring Boot应用。若是您对于如何构建还不熟悉,能够参考《使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程》一文。spring

  • pom.xml引入以下依赖(若是使用Intellij中的Spring Initializr的话直接在提示框中选下web和actuator模块便可)。安全

    <parent>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
    	<version>1.5.1.RELEASE</version>
    	<relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <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>
    </dependencies>

    在应用主类中添加一个接口用来测试日志级别的变化,好比下面的实现:app

    @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);
    	}
      
    }

    为了后续的试验顺利,在application.properties中增长一个配置,来关闭安全认证校验。spring-boot

    management.security.enabled=false

    否则在访问/loggers端点的时候,会报以下错误:测试

    {
      "timestamp": 1485873161065,
      "status": 401,
      "error": "Unauthorized",
      "message": "Full authentication is required to access this resource.",
      "path": "/loggers/com.didispace"
    }

    测试验证ui

    在完成了上面的构建以后,咱们启动示例应用,并访问/test端点,咱们能够在控制台中看到以下输出:this

    2017-01-31 22:34:57.123  INFO 16372 --- [nio-8000-exec-1] ication$$EnhancerBySpringCGLIB$$d2a0b1e2 : Logger Level :INFO
    2017-01-31 22:34:57.124 ERROR 16372 --- [nio-8000-exec-1] ication$$EnhancerBySpringCGLIB$$d2a0b1e2 : Logger Level :ERROR

    因为默认的日志级别为INFO,因此并无输出DEBUG级别的内容。下面咱们能够尝试经过/logger端点来将日志级别调整为DEBUG,好比,发送POST请求到/loggers/com.didispace端点,其中请求体Body内容为:spa

    {
        "configuredLevel": "DEBUG"
    }

    从新访问/test端点,咱们将在控制台中看到以下输出,在/test端点中定义的DEBUG日志内容被打印了出来:

    2017-01-31 22:37:35.252 DEBUG 16372 --- [nio-8000-exec-5] ication$$EnhancerBySpringCGLIB$$d2a0b1e2 : Logger Level :DEBUG
    2017-01-31 22:37:35.252  INFO 16372 --- [nio-8000-exec-5] ication$$EnhancerBySpringCGLIB$$d2a0b1e2 : Logger Level :INFO
    2017-01-31 22:37:35.252 ERROR 16372 --- [nio-8000-exec-5] ication$$EnhancerBySpringCGLIB$$d2a0b1e2 : Logger Level :ERROR

    能够看到,到这里为止,咱们并无重启过Spring Boot应用,而只是简单的经过调用/loggers端点就能控制日志级别的更新。除了POST请求以外,咱们也能够经过GET请求来查看当前的日志级别设置,好比:发送GET请求到/loggers/com.didispace端点,咱们将得到对于com.didispace包的日志级别设置:

    {
      "configuredLevel": "DEBUG",
      "effectiveLevel": "DEBUG"
    }

    咱们也能够不限定条件,直接经过GET请求访问/loggers来获取全部的日志级别设置,这里就不列举具体返回,读者能够自行尝试。

  • 源码来源

相关文章
相关标签/搜索