Spring Boot 1.5:動態修改日志級別

/loggers是Spring Boot 1.5中引入的一个控制端点,它提供了动态修改日志级别的功能。只需要引入spring-boot-starter-actuator依赖便自动开启该功能。

代码

pom.xml中引入如下依赖:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <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>

编写一个简单的Spring Boot程序,添加一个Controller来打印不同级别的日志:

package com.makazeu.springdemo1;

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 SpringDemo1Application {

  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 "testing logger";
  }

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

为了在不引入Spring Security的情况下允许访问Actuator,还需要在配置文件application.properties中加入一条配置项:

management.security.enabled=false

测试

访问/test,可以看到控制台有如下输出:

2018-02-26 00:39:41.009  INFO 16632 --- [nio-8089-exec-8] ication$$EnhancerBySpringCGLIB$$8904f7e9 : Logger Level: INFO
2018-02-26 00:39:41.009 ERROR 16632 --- [nio-8089-exec-8] ication$$EnhancerBySpringCGLIB$$8904f7e9 : Logger Level: Error

默认的日志级别为INFO,因此输出了INFOERROR级别的日志。

通过HTTP GET loggers/com.makazeu,可以得到如下结果:

{
    "configuredLevel": null,
    "effectiveLevel": "INFO"
}

此时HTTP POST loggers/com.makazeu,内容为如下:

{
    "configuredLevel": "ERROR"
}

便可将包com.makazeu内的日志级别改为ERROR。再次HTTP GET loggers/com.makazeu,可以得到如下结果:

{
    "configuredLevel": "ERROR",
    "effectiveLevel": "ERROR"
}

关于子包继承的问题,若子包未配置日志级别,则继承父包的配置;若子包已配置,则子包的日志级别优先。如com.makazeu.springdemo1的日志级别为ERROR,把com.makazeu的日志级别修改为DEBUG后,子包com.makazeu.springdemo1的日志级别仍为ERROR

References: