序言
Spring Boot 在 spring-boot-starter-actuator 模塊中提供了日誌相關的 EndPoint,通過該 EndPoint 可以在運行時不需要重啓服務就可以修改日誌的打印級別。
解決了以前修改日誌打印級別必須要重啓服務的煩惱。
環境信息
- spring boot 2.0.4.RELEASE(1.5以前的版本不適用該博文)
- JDK 1.8
POM 信息
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
啓用loggers Endpoint
出於安全因素考慮,依賴spring-boot-starter-actuator後,默認值啓用了“/health”和“/info”兩個 Endpoint
可以通過如下配置啓用所有的 endpoint
management.endpoints.web.exposure.include=*
也可以通過該配置啓用指定的 endpoint
management.endpoints.web.exposure.include=loggers,sessions
查看日誌級別
我們可以通過瀏覽器請求到 http://127.0.0.1:8080/actuator/loggers 來獲取支持的日誌等級,以及系統默認的日誌等和各個包路徑對應的日誌級別。
ps: 如下結果是精簡過的,實際返回值很多
{
"levels":[
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers":{
"ROOT":{
"configuredLevel":"INFO",
"effectiveLevel":"INFO"
},
"com":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.github":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.github.huotaihe":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.github.huotaihe.spring":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.github.huotaihe.spring.boot":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.github.huotaihe.spring.boot.log":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.github.huotaihe.spring.boot.log.Application":{
"configuredLevel":null,
"effectiveLevel":"INFO"
}
}
}
配置日誌級別
編寫日誌輸出類
編寫一個controller 輸出各個級別的日誌:
注意:日誌門面類要使用 commons-log,而不能使用 sl4j;
使用 sl4j 會導致日誌級別不生效,具體原因會繼續跟進。
package com.github.huotaihe.spring.boot.log;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LogController {
private final static Logger LOG = LoggerFactory.getLogger(LogController.class);
@GetMapping("/log")
public Map<String, Object> home() {
if(LOG.isTraceEnabled()){
LOG.trace("trace level log");
}
if(LOG.isDebugEnabled()){
LOG.debug("debug level log");
}
if(LOG.isInfoEnabled()){
LOG.info("info level log");
}
if(LOG.isWarnEnabled()){
LOG.warn("warn level log");
}
if(LOG.isErrorEnabled()){
LOG.error("error level log");
}
Map<String, Object> result = new HashMap<>();
result.put("status", "good");
result.put("name", "abc");
result.put("password", "abc");
return result;
}
}
測試日誌級別
啓動應用並訪問http://127.0.0.1:8080/log
控制檯打印如圖,Spring Boot 默認的 ROOT 日誌級別是INFO。
修改指定包的日誌級別
通過/actuator/loggers端點提供的 POST 請求,修改包路徑com.github.huotaihe.spring.boot.log 的日誌級別爲DEBUG。
header 信息必須添加
Content-Type:application/json
- 瀏覽器通過 GET 方式訪問如下地址
http://127.0.0.1:8080/actuator/loggers/com.github.huotaihe.spring.boot.log
查看當前包的目前日誌級別
{
"configuredLevel": "DEBUG",
"effectiveLevel": "DEBUG"
}
- 再次訪問http://127.0.0.1:8080/log 得到