• Spring Boot Actuator 管理日志


    为了解决以下两个问题:

    1、单JAR包应用查看日志需要的时候如果需要远程访问服务器登录查看日志,那样相对比较麻烦

    2、生产环境为了解决BUG需要临时更换日志级别,总不能重启服务来解决吧

    所以使用了actuator 其中的部分来解决这两个问题。

    首先在POM文件中引入actuator依赖:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-actuator</artifactId>
    4. <version>${spring-boot.version}</version>
    5. </dependency>

    配置文件中配置:

    1. management.endpoints.web.base-path=/actuator
    2. management.endpoints.web.exposure.include=logfile,loggers
    3. management.endpoint.health.show-details=always
    4. 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:8085/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8085/actuator/loggers/{name}","templated":true}}}

    其中

    logfile 是查看日志文件

    loggers是查看日志级别

    loggers/{name}是更改日志级别

    前端参考代码:

    1. <TabPane label="接口日志" name="name3">
    2. 级别:
    3. <RadioGroup v-model="loglevel" type="button" size="small" @on-
    4. change="lvChange()">
    5. <Radio label="ERROR"></Radio>
    6. <Radio label="INFO"></Radio>
    7. <Radio label="DEBUG"></Radio>
    8. </RadioGroup> <br/><br/>
    9. 文件:<a :href="logfileurl" target="_blank" > 查看</a>
    10. </TabPane>
    11. this.logfileurl = res.dataApi+"actuator/logfile";
    12. this.loglevelurl = res.dataApi+"actuator/loggers/root";
    13. getLogLevel(){
    14. this.ajax_get({
    15. url: this.loglevelurl,
    16. params: {},
    17. }).then((res) => {
    18. this.loglevel=res.configuredLevel
    19. });
    20. },
    21. lvChange(){
    22. this.changeLogLevel(this.loglevel)
    23. },
    24. changeLogLevel(level){
    25. this.ajax_post({
    26. url: this.tenant.dataApi + "actuator/loggers/root",
    27. params: {'configuredLevel':level},
    28. }).then((res) => {
    29. this.spinShow = false;
    30. if (!res.code) {
    31. this.$Notice.success({
    32. title:'更改日志级别为'+level,
    33. desc:res.msg
    34. });
    35. } else {
    36. this.$Notice.error({
    37. title:'更改日志级别失败',
    38. desc:res.msg
    39. });
    40. }
    41. });
    42. }

    最终效果如下:

     

     

  • 相关阅读:
    【兴业数金】其他面试题整理
    契约锁“7大”签约避坑指南,帮您化解99%的合同签署麻烦
    爬虫(1) - 爬虫基础入门理论篇
    MBR16200FCT-ASEMI大功率肖特基二极管MBR16200FCT
    SQL获取IP电脑名
    JVM详细教程
    【算法专题--链表】两两交换链表中的节点 -- 高频面试题(图文详解,小白一看就懂!!!)
    输入输出系统
    2022年中纪实 -- 一个普通人的经历
    Django系列8-员工管理系统实战--部门管理
  • 原文地址:https://blog.csdn.net/chengmin123456789/article/details/125597951