為了解決以下兩個問題:
1、單JAR套件應用查看日誌需要的時候如果需要遠端存取伺服器登入查看日誌,那樣相對比較麻煩
2、生產環境為了解決BUG需要臨時更換日誌級別,總不能重啟服務來解決吧
所以使用了actuator 其中的部分來解決這兩個問題。
首先在POM檔案中引入actuator依賴:
1 2 3 4 5 | <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
|
登入後複製
設定檔中設定:
1 2 3 4 | management.endpoints.web.base-path=/actuator
management.endpoints.web.exposure. include =logfile,loggers
management.endpoint.health.show-details=always
logging.file.name=logs/EL-3KJ/EL-3KJ.log
|
登入後複製
然後直接可以存取 http://localhost:8085/actuator
得到下列結果:
{"_links":{
"self"{"href":"http://localhost:8085/actuator","templated":false },
"logfile: {"href":"http://localhost:8085/actuator/logfile","templated":false},"loggers":{"href":"http://localhost:"http://localhost: 8085/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8085/actuator/loggers/{name}","templated":true}} }
其中
logfile 是檢視日誌檔案
loggers是檢視日誌等級
loggers/{name}是更改日誌等級
前端參考程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <TabPane label= "接口日志" name= "name3" >
级别:
<RadioGroup v-model= "loglevel" type= "button" size= "small" @on-
change= "lvChange()" >
<Radio label= "ERROR" ></Radio>
<Radio label= "INFO" ></Radio>
<Radio label= "DEBUG" ></Radio>
</RadioGroup> <br/><br/>
文件:<a :href= "logfileurl" rel= "external nofollow" target= "_blank" > 查看</a>
</TabPane>
this.logfileurl = res.dataApi+ "actuator/logfile" ;
this.loglevelurl = res.dataApi+ "actuator/loggers/root" ;
getLogLevel(){
this.ajax_get({
url: this.loglevelurl,
params: {},
}).then((res) => {
this.loglevel=res.configuredLevel
});
},
lvChange(){
this.changeLogLevel(this.loglevel)
},
changeLogLevel(level){
this.ajax_post({
url: this.tenant.dataApi + "actuator/loggers/root" ,
params: {'configuredLevel':level},
}).then((res) => {
this.spinShow = false;
if (!res.code) {
this. $Notice .success({
title:'更改日志级别为'+level,
desc:res.msg
});
} else {
this. $Notice .error({
title:'更改日志级别失败',
desc:res.msg
});
}
});
}
|
登入後複製
最終效果如下:
![Spring Boot Actuator管理日誌如何實現](/static/imghw/default1.png)
![Spring Boot Actuator管理日誌如何實現](/static/imghw/default1.png)
以上是Spring Boot Actuator管理日誌如何實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!