SpringBoot動(dòng)態(tài)修改日志級(jí)別的操作
為了減少日志頻繁打印帶來的性能影響,線上環(huán)境設(shè)置的日志級(jí)別一般都相對(duì)較高。而當(dāng)出現(xiàn)生產(chǎn)問題需要排查的時(shí)候,可能需要適當(dāng)降低日志級(jí)別(例如DEBUG)來打印更多的日志信息幫助定位問題。
傳統(tǒng)的做法一般是:1、配置里修改日志級(jí)別
2、重啟應(yīng)用
3、問題復(fù)現(xiàn)查看報(bào)錯(cuò)日志排查問題
這個(gè)過程需要重啟應(yīng)用,比較麻煩,效率較低,而且針對(duì)大型在線項(xiàng)目,不可能隨便停機(jī)重啟。那么有沒有一種方式在不重啟應(yīng)用的情況下實(shí)現(xiàn)動(dòng)態(tài)修改日志級(jí)別呢?
下面,讓老萬教你如何通過SpringBoot的actuator組件來實(shí)現(xiàn)動(dòng)態(tài)修改日志級(jí)別。
一、添加依賴<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>二、配置actuator暴露的端口
#啟用actuator端口management.endpoints.enabled-by-default=fasle#設(shè)置actuator的訪問根路徑,默認(rèn)是/actuatormanagement.endpoints.web.base-path=/message#啟用的端點(diǎn)management.endpoints.web.exposure.include=loggers
這里我修改了actuator的默認(rèn)訪問路徑/actuator,改為/message,為的是和項(xiàng)目的基礎(chǔ)訪問路徑保存一致。
啟用端口的2中配置方法:方式一:(推薦)
management.endpoints.web.exposure.include=loggers
方式二:(這種方式測(cè)試沒有生效)
management.endpoint.loggers.enabled=true
補(bǔ)充:如何禁用info端口
management.endpoints.enabled-by-default=falsemanagement.endpoint.info.enabled=true
關(guān)于actuator組件被稱為spring boot的4大組件之一,功能強(qiáng)大,大家在網(wǎng)上自己找些資料進(jìn)一步了解。
actuator的endpoint端口說明:ID 描述 默認(rèn)啟用 auditevents 顯示當(dāng)前應(yīng)用程序的審計(jì)事件信息 Yes beans 顯示一個(gè)應(yīng)用中所有Spring Beans的完整列表 Yes conditions 顯示配置類和自動(dòng)配置類(configuration and auto-configuration classes)的狀態(tài)及它們被應(yīng)用或未被應(yīng)用的原因 Yes configprops 顯示一個(gè)所有@ConfigurationProperties的集合列表 Yes env 顯示來自Spring的 ConfigurableEnvironment的屬性 Yes flyway 顯示數(shù)據(jù)庫遷移路徑,如果有的話 Yes health 顯示應(yīng)用的健康信息(當(dāng)使用一個(gè)未認(rèn)證連接訪問時(shí)顯示一個(gè)簡(jiǎn)單的’status’,使用認(rèn)證連接訪問則顯示全部信息詳情) Yes info 顯示任意的應(yīng)用信息 Yes liquibase 展示任何Liquibase數(shù)據(jù)庫遷移路徑,如果有的話 Yes metrics 展示當(dāng)前應(yīng)用的metrics信息 Yes mappings 顯示一個(gè)所有@RequestMapping路徑的集合列表 Yes scheduledtasks 顯示應(yīng)用程序中的計(jì)劃任務(wù) Yes sessions 允許從Spring會(huì)話支持的會(huì)話存儲(chǔ)中檢索和刪除(retrieval and deletion)用戶會(huì)話。使用Spring Session對(duì)反應(yīng)性Web應(yīng)用程序的支持時(shí)不可用。 Yes shutdown 允許應(yīng)用以優(yōu)雅的方式關(guān)閉(默認(rèn)情況下不啟用) No threaddump 執(zhí)行一個(gè)線程dump Yes 如果使用web應(yīng)用(Spring MVC, Spring WebFlux, 或者 Jersey),你還可以使用以下端點(diǎn):ID 描述 默認(rèn)啟用 heapdum 返回一個(gè)GZip壓縮的hprof堆dump文件 Yes jolokia 通過HTTP暴露JMX beans(當(dāng)Jolokia在類路徑上時(shí),WebFlux不可用) Yes logfile 返回日志文件內(nèi)容(如果設(shè)置了logging.file或logging.path屬性的話),支持使用HTTP Range頭接收日志文件內(nèi)容的部分信息 Yes prometheus 以可以被Prometheus服務(wù)器抓取的格式顯示metrics信息 Yes 要更改公開哪些端點(diǎn),請(qǐng)使用以下技術(shù)特定的include和exclude屬性:Property Default management.endpoints.jmx.exposure.exclude * management.endpoints.jmx.exposure.include * management.endpoints.web.exposure.exclude * management.endpoints.web.exposure.include info, healthinclude屬性列出了公開的端點(diǎn)的ID,
exclude屬性列出了不應(yīng)該公開的端點(diǎn)的ID
exclude屬性優(yōu)先于include屬性。包含和排除屬性都可以使用端點(diǎn)ID列表進(jìn)行配置。
*可以用來選擇所有端點(diǎn)。
例如,要通過HTTP公開除env和beans端點(diǎn)之外的所有內(nèi)容,請(qǐng)使用以下屬性:
management.endpoints.web.exposure.include=*management.endpoints.web.exposure.exclude=env,beans三、關(guān)閉鑒權(quán)
一般我們會(huì)將actuator和spring security鑒權(quán)組件結(jié)合使用,防止這些功能端口被隨便調(diào)用。由于這里是功能演示,先放開actuator相關(guān)端口的權(quán)限認(rèn)證。
此外,如果存在Spring Security,則需要添加自定義安全配置,以允許對(duì)端點(diǎn)進(jìn)行未經(jīng)身份驗(yàn)證的訪問,如以下示例所示:放開所有Endpoint端點(diǎn)進(jìn)行匹配
@Configurationpublic class ActuatorSecurity extends WebSecurityConfigurerAdapter { @Overrideprotected void configure(HttpSecurity http) throws Exception { http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests() .anyRequest().permitAll()}}四 、通過/loggers端口查看日志級(jí)別
請(qǐng)求鏈接:http://localhost:8090/message/loggers
注意上面我說過的,我調(diào)整了management.endpoints.web.base-path=/message。如果沒有設(shè)置此參數(shù),則使用默認(rèn)的/actuator去訪問。
這里演示,修改目錄com.wxswj.provider.message.controller的日志級(jí)別為debug
請(qǐng)求類型為POST,參數(shù)格式是JSON
curl -H 'Content-Type: application/json' -X POST --data ’{ 'configuredLevel': 'DEBUG'}’ http://localhost:8090/message/loggers/com.wxswj.provider.message.controller
大家可以在服務(wù)器上通過curl發(fā)起http請(qǐng)求,或者通過Postman發(fā)起請(qǐng)求。
curl -H 'Content-Type: application/json' -X POST --data ’{'configuredLevel': 'DEBUG'}’ http://localhost:8090/loggers/com.wxswj.provider.message.controller六、查詢?nèi)罩炯?jí)別修改結(jié)果
http://localhost:8090/message/loggers/com.wxswj.provider.message.controller
{'configuredLevel': 'DEBUG','effectiveLevel': 'DEBUG'}
說明我們的修改日志級(jí)別的請(qǐng)求生效。
總結(jié)通過整合spring boot的actuator組件,公開對(duì)應(yīng)的/loggers端口,我們就可以輕松的實(shí)現(xiàn)動(dòng)態(tài)調(diào)整系統(tǒng)的日志級(jí)別,而不用項(xiàng)目重啟。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
