spring1.5.X版本引入的一個(gè)新的控制端點(diǎn):/loggers
湿酸,該端點(diǎn)將為我們提供動(dòng)態(tài)修改Spring Boot應(yīng)用日志級(jí)別的強(qiáng)大功能臂外。該功能的使用非常簡(jiǎn)單,它依然延續(xù)了Spring Boot自動(dòng)化配置的實(shí)現(xiàn)研乒,所以只需要在引入了spring-boot-starter-actuator依賴的條件下就會(huì)自動(dòng)開(kāi)啟該端點(diǎn)的功能(更多關(guān)于spring-boot-starter-actuator
模塊的詳細(xì)介紹可見(jiàn):《springboot中使用actuator進(jìn)行監(jiān)控》一文)止吁。
配置:
pom依賴:
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
yml配置:
##運(yùn)行狀態(tài) actuator監(jiān)控
endpoints:
loggers:
enabled: true
sensitive: false
management:
##服務(wù)路徑
context-path: /manage
##服務(wù)端口
port: 8081
然后啟動(dòng)項(xiàng)目,可以看到端口已經(jīng)映射成功
... o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/manage/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
... o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/manage/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>)
... o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/manage/loggers || /manage/loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
查詢?nèi)罩炯?jí)別:
使用GET
請(qǐng)求:
/manage/loggers/
會(huì)返回所有的日志級(jí)別:
{
"levels":[
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers":{
"ROOT":{
"configuredLevel":"INFO",
"effectiveLevel":"INFO"
},
"com":{
"configuredLevel":null,
"effectiveLevel":"INFO"
},
"com.caiyi":{
"configuredLevel":null,
"effectiveLevel":"INFO"
}
...
}
}
修改日志級(jí)別:
使用POST
請(qǐng)求:
/manage/loggers/{elephant}
{elephant}為前面查詢到的目錄挽封。
比如我修改com.caiyi
下面的日志級(jí)別為debug已球,訪問(wèn):
http:127.0.0.1:8081/manage/loggers/com.caiyi
請(qǐng)求body中參數(shù):
{
"configuredLevel": "debug"
}
然后再調(diào)用查詢接口就會(huì)發(fā)現(xiàn)已經(jīng)改為debug
級(jí)別了