本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x全部版本。java
做为程序猿,定位问题是咱们的平常工做,而日志是咱们定位问题很是重要的依据。传统方式定位问题时,每每是以下步骤:git
DEBUG
;若是能动态修改日志级别(无需重启应用,就能马上刷新),那绝对 如猫添翼
。事实上,从 Spring Boot 1.5
开始,Spring Boot Actuator
组件就已提供动态修改日志级别的能力。github
TIPSweb
- 其实更低版本也只需简单扩展,便可实现动态修改日志级别。
- 对Spring Boot Actuator感到陌生的童鞋,可先前往 Spring Boot Actuator 了解基础用法。
废话很少说了,亮代码吧。spring
加依赖apache
<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>
这里的 spring-boot-starter-web
不是必须的,只是下面测试代码要用到。json
写代码app
package com.itmuch.logging; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author itmuch.com */ @RestController public class TestController { private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class); @GetMapping("/test") public String simple() { LOGGER.debug("这是一个debug日志..."); return "test"; } }
写配置:curl
management: endpoints: web: exposure: include: 'loggers'
因为Spring Boot 2.x默认只暴露 /health
以及 /info
端点,而日志控制须要用到 /loggers
端点,故而须要设置将其暴露。ide
代码编写完成啦。
/loggers
端点提供了 查看
以及 修改
日志级别的能力。
访问 http://localhost:8080/actuator/loggers
,可看到相似以下的结果:
{ "levels": ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"], "loggers": { "ROOT": { "configuredLevel": "INFO", "effectiveLevel": "INFO" }, "com.itmuch.logging.TestController": { "configuredLevel": null, "effectiveLevel": "INFO" } } // ...省略 }
访问 http://localhost:8080/actuator/loggers/com.itmuch.logging.TestController
,可看到相似以下的结果:
{"configuredLevel":null,"effectiveLevel":"INFO"}
由测试不难发现,想看哪一个包/类的日志,只需构造 /actuator/loggers/包名类名全路径
去访问便可。
在 TestController
类中,笔者编写设置了一条日志 LOGGER.debug("这是一个debug日志...");
,而由测试1,默认的日志级别是INFO,因此不会打印。下面来尝试将该类的日志级别设为DEBUG。
curl -X POST http://localhost:8080/actuator/loggers/com.itmuch.logging.TestController \ -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8" \ --data '{"configuredLevel":"debug"}'
如上,只需发送一个POST请求,并将请求body设为:{"configuredLevel":"debug"}
便可。
此时,访问 localhost:8080/test
会看到相似以下的日志:
2019-03-28 16:24:04.513 DEBUG 19635 --- [nio-8080-exec-7] com.itmuch.logging.TestController : 这是一个debug日志...
而且,此时再访问 http://localhost:8080/actuator/loggers/com.itmuch.logging.TestController
,可看到相似以下的结果:
{"configuredLevel":"DEBUG","effectiveLevel":"DEBUG"}
说明已成功动态修改日志级别。
TIPS
本节着重分析如何实现动态修改。
Actuator有约定, /actuator/xxx 端点的定义代码在 xxxEndpoint 中。故而,找到类 org.springframework.boot.actuate.logging.LoggersEndpoint
,可看到相似以下的代码:
@Endpoint(id = "loggers") public class LoggersEndpoint { private final LoggingSystem loggingSystem; @WriteOperation public void configureLogLevel(@Selector String name, @Nullable LogLevel configuredLevel) { Assert.notNull(name, "Name must not be empty"); this.loggingSystem.setLogLevel(name, configuredLevel); } // ...其余省略 }
其中, Endpoint
、WriteOperation
、@Selector
都是Spring Boot 2.0开始提供的新注解。
@Endpoint(id = "loggers")
用来描述Spring Boot Actuator
的端点,这样就会产生一个/actuator/loggers
的路径,它相似于Spring MVC的 @RequestMapping("loggers")
。
@WriteOperation
表示这是一个写操做,它相似于Spring MVC的 @PostMapping
。Spring Boot Actuator还提供了其余操做,以下表:
Operation | HTTP method |
---|---|
@ReadOperation |
GET |
@WriteOperation |
POST |
@DeleteOperation |
DELETE |
@Selector
用于筛选 @Endpoint
注解返回值的子集,它相似于Spring MVC的 @PathVariable
。
这样,上面的代码就很好理解了—— configureLogLevel
方法里面就一行代码 :this.loggingSystem.setLogLevel(name, configuredLevel);
,发送POST请求后,name就是咱们传的包名或者类名,configuredLevel就是咱们传的消息体。
怎么实现动态修改的呢?不妨点进去看看,而后发现代码以下:
// org.springframework.boot.logging.LoggingSystem#setLogLevel public void setLogLevel(String loggerName, LogLevel level) { throw new UnsupportedOperationException("Unable to set log level"); }
嘿嘿,没事,确定有实现类, 该方法在以下实现类被实现:
# 适用于java.util.logging的LoggingSystem org.springframework.boot.logging.java.JavaLoggingSystem # 适用于Log4j 2的LoggingSystem org.springframework.boot.logging.log4j2.Log4J2LoggingSystem # 适用于logback的LoggingSystem org.springframework.boot.logging.logback.LogbackLoggingSystem # 啥都不干的LoggingSystem org.springframework.boot.logging.LoggingSystem.NoOpLoggingSystem
Spring Boot 2.x中,默认使用Logback,所以进入到 LogbackLoggingSystem
中,代码以下:
@Override public void setLogLevel(String loggerName, LogLevel level) { ch.qos.logback.classic.Logger logger = getLogger(loggerName); if (logger != null) { logger.setLevel(LEVELS.convertSystemToNative(level)); } }
至此,就真相大白了。其实根本没有黑科技,Spring Boot本质上仍是使用了Logback的API,ch.qos.logback.classic.Logger.setLevel
实现日志级别的修改。
你可能会好奇,LoggingSystem有这么多实现类,Spring Boot怎么知道什么状况下用什么LoggingSystem呢?可在 org.springframework.boot.logging.LoggingSystem
找到相似以下代码:
public abstract class LoggingSystem { private static final Map<String, String> SYSTEMS; static { Map<String, String> systems = new LinkedHashMap<>(); systems.put("ch.qos.logback.core.Appender", "org.springframework.boot.logging.logback.LogbackLoggingSystem"); systems.put("org.apache.logging.log4j.core.impl.Log4jContextFactory", "org.springframework.boot.logging.log4j2.Log4J2LoggingSystem"); systems.put("java.util.logging.LogManager", "org.springframework.boot.logging.java.JavaLoggingSystem"); SYSTEMS = Collections.unmodifiableMap(systems); } /** * Detect and return the logging system in use. Supports Logback and Java Logging. * @param classLoader the classloader * @return the logging system */ public static LoggingSystem get(ClassLoader classLoader) { String loggingSystem = System.getProperty(SYSTEM_PROPERTY); if (StringUtils.hasLength(loggingSystem)) { if (NONE.equals(loggingSystem)) { return new NoOpLoggingSystem(); } return get(classLoader, loggingSystem); } return SYSTEMS.entrySet().stream() .filter((entry) -> ClassUtils.isPresent(entry.getKey(), classLoader)) .map((entry) -> get(classLoader, entry.getValue())).findFirst() .orElseThrow(() -> new IllegalStateException( "No suitable logging system located")); } // 省略不相关内容... }
由代码不难发现,其实就是构建了一个名为 SYSTEMS
的map,做为各类日志系统的字典;而后在 get
方法中,看应用是否加载了map中的类;若是加载了,就经过反射,初始化响应 LoggingSystem
。例如:Spring Boot发现当前应用加载了 ch.qos.logback.core.Appender
,就去实例化 org.springframework.boot.logging.logback.LogbackLoggingSystem
。
本文是使用 curl
手动发送 POST
请求手动修改日志级别的,该方式不适用生产,由于很麻烦,容易出错。生产环境,建议根据Actuator提供的RESTful API定制界面,或使用 Spring Boot Admin
,可视化修改日志级别,以下图所示:
想修改哪一个包/类的日志级别,直接点击便可。
GitHub:https://github.com/eacdy/spring-boot-study/tree/master/spring-boot-logging-change-logging-level
Gitee:https://gitee.com/itmuch/spring-boot-study/tree/master/spring-boot-logging-change-logging-level
http://www.itmuch.com/spring-boot/change-logger-level/