Spring Boot 动态配置日志级别

来源:互联网 发布:pc安装mac os x系统 编辑:程序博客网 时间:2024/06/10 16:45

摘要

Spring Boot 在日志的配置上为我们做了众多的自动化工作,详细参见《寻根究底 Logging - Spring Boot 日志概述》。
从版本 1.5.1 之后就提供了基于 spring-boot-starter-actuator 的端点 /loggers。通过该端点可以实现查看系统的 package-path 的日志级别,以及针对特定的 package-path 配置运行中的应用的日志级别的功能。

Actuator 依赖

pom 依赖

因为是基于 Web 的项目和利用 Actuator 提供的端点来进行配置,因此需要依赖:

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

安全配置

Actuator 提供的端点(Endpoints),默认是需要安全认证才能够被访问的。因为里面涉及到一些敏感的信息。需要安全认证就需要配置 Spring-Security。为了方便首先配置不需要安全权限的。

management.security.enabled=false

GET 请求访问

我们可以发送GET 请求到 http://localhost:8080/loggers 来获取支持的日志等级,以及系统默认的日志等和各个包路径对应的日志级别。

{  levels: [    "OFF",    "ERROR",    "WARN",    "INFO",    "DEBUG",    "TRACE"  ],  loggers: {    ROOT: {      configuredLevel: "INFO",      effectiveLevel: "INFO"    },    cn: {      configuredLevel: null,      effectiveLevel: "INFO"    },    cn.cincout: {      configuredLevel: null,      effectiveLevel: "INFO"    },    cn.cincout.tech: {      configuredLevel: null,      effectiveLevel: "INFO"    },    cn.cincout.tech.black: {      configuredLevel: null,      effectiveLevel: "INFO"    },    cn.cincout.tech.black.hole: {      configuredLevel: null,      effectiveLevel: "INFO"    },    cn.cincout.tech.black.hole.springboot: {      configuredLevel: null,      effectiveLevel: "INFO"    },    cn.cincout.tech.black.hole.springboot.SpringBootStartApplication: {      configuredLevel: null,      effectiveLevel: "INFO"    },    cn.cincout.tech.black.hole.springboot.application: {      configuredLevel: null,      effectiveLevel: "INFO"    }  }}

配置日志级别

编写日志输出类

编写一个controller 输出各个级别的日志:

package cn.cincout.tech.black.hole.springboot.interfaces.web;@Controllerpublic class HomeController {    private final static Logger LOG = LoggerFactory.getLogger(HomeController.class);    @Value("${val.name}")    private String name;    @Value("${val.password}")    private String password;    @GetMapping(value = {"", "/"})    @ResponseBody    public Map<String, Object> home() {        LOG.trace("trace level log");        LOG.debug("debug level log");        LOG.info("info level log");        LOG.warn("warn level log");        LOG.error("error level log");        Map<String, Object> result = new HashMap<>();        result.put("status", "good");        result.put("name", name);        result.put("password", password);        return result;    }    @GetMapping(value = "/admin")    public String shutdown() {        return "shutdown";    }}

查看日志级别

启动应用访问 http://localhost:8080/ 得到:

2017-09-01 10:19:16.180  INFO 6954 --- [nio-8443-exec-4] c.c.t.b.h.s.i.web.HomeController         : info level log2017-09-01 10:19:16.180  WARN 6954 --- [nio-8443-exec-4] c.c.t.b.h.s.i.web.HomeController         : warn level log2017-09-01 10:19:16.180 ERROR 6954 --- [nio-8443-exec-4] c.c.t.b.h.s.i.web.HomeController         : error level log

Spring Boot 默认的 ROOT 日志级别是INFO

配置特定包的日志级别

通过 /loggers 端点提供的 POST 请求,修改包路径cn.cincout.tech.black.hole.springboot.interfaces.web 的日志级别为DEBUG
* 发送POST 请求到 /loggers/cn.cincout.tech.black.hole.springboot.interfaces.web,其中请求 Body 的内容如下:

{  "configuredLevel": "DEBUG"}
  • GET 访问 /loggers/cn.cincout.tech.black.hole.springboot.interfaces.web 查看当前的日志级别:
{  configuredLevel: "DEBUG",  effectiveLevel: "DEBUG"}
  • 再次访问 http://localhost:8080/ 得到:
2017-09-01 11:15:43.695 DEBUG 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : debug level log2017-09-01 11:15:43.695  INFO 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : info level log2017-09-01 11:15:43.695  WARN 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : warn level log2017-09-01 11:15:43.695 ERROR 6954 --- [nio-8443-exec-1] c.c.t.b.h.s.i.web.HomeController         : error level log

需要注意的是,通过 /loggers 配置的日志级别在应用重启时会恢复到系统的配置。如果想永久的配置日志的级别还是需要通过logging.level.package-path 来进行配置。

源代码

本工程源代码可以从github 获取。源代码

总结

Spring Boot 提供的日志级别动态配置功能,为我们的线上应用调试提供了很好的机制。在实际使用中需要结合 Spring-Security 提供的安全机制来保护Actuator 提供的各种系统级端点。想了解更多关于 Java 日志的内容参见《寻根究底 Logging - SLF4J 日志Facade 概述》。

参考

  1. Configure a Logger

原文地址《Spring Boot 动态配置日志级别》

我的博客 http://tramp.cincout.cn/

原创粉丝点击