您好,欢迎访问一九零五行业门户网

Spring Boot Actuator管理日志如何实现

为了解决以下两个问题:
1、单jar包应用查看日志需要的时候如果需要远程访问服务器登录查看日志,那样相对比较麻烦
2、生产环境为了解决bug需要临时更换日志级别,总不能重启服务来解决吧
所以使用了actuator 其中的部分来解决这两个问题。
首先在pom文件中引入actuator依赖:
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> <version>${spring-boot.version}</version> </dependency>
配置文件中配置:
management.endpoints.web.base-path=/actuatormanagement.endpoints.web.exposure.include=logfile,loggers management.endpoint.health.show-details=alwayslogging.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:8085/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8085/actuator/loggers/{name}","templated":true}}}
其中
logfile 是查看日志文件
loggers是查看日志级别
loggers/{name}是更改日志级别
前端参考代码:
<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管理日志如何实现的详细内容。
其它类似信息

推荐信息