springboot動態修改系統日誌級別

spring1.5.X版本引入的一個新的控制端點:/loggers,該端點將爲我們提供動態修改Spring Boot應用日誌級別的強大功能。該功能的使用非常簡單,它依然延續了Spring Boot自動化配置的實現,所以只需要在引入了spring-boot-starter-actuator依賴的條件下就會自動開啓該端點的功能(更多關於spring-boot-starter-actuator
模塊的詳細介紹可見:《springboot中使用actuator進行監控》一文)。

配置:

pom依賴:

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

yml配置:

##運行狀態 actuator監控
endpoints:
  loggers:
    enabled: true
    sensitive: false
management:
  ##服務路徑
  context-path: /manage
  ##服務端口
  port: 8081

然後啓動項目,可以看到端口已經映射成功

... 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()

查詢日誌級別:

使用GET請求:

/manage/loggers/

會返回所有的日誌級別:

{
    "levels":[
        "OFF",
        "ERROR",
        "WARN",
        "INFO",
        "DEBUG",
        "TRACE"
    ],
    "loggers":{
        "ROOT":{
            "configuredLevel":"INFO",
            "effectiveLevel":"INFO"
        },
        "com":{
            "configuredLevel":null,
            "effectiveLevel":"INFO"
        },
        "com.caiyi":{
            "configuredLevel":null,
            "effectiveLevel":"INFO"
        }
        ...
    }
}

修改日誌級別:

使用POST請求:

/manage/loggers/{elephant}

{elephant}爲前面查詢到的目錄。
比如我修改com.caiyi下面的日誌級別爲debug,訪問:

http:127.0.0.1:8081/manage/loggers/com.caiyi

請求body中參數:

{
    "configuredLevel": "debug"
}

然後再調用查詢接口就會發現已經改爲debug級別了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章