首頁 > Java > java教程 > Spring Boot Actuator管理日誌如何實現

Spring Boot Actuator管理日誌如何實現

王林
發布: 2023-05-12 18:01:12
轉載
1168 人瀏覽過

為了解決以下兩個問題:

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: {&#39;configuredLevel&#39;:level},

      }).then((res) => {

        this.spinShow = false;

        if (!res.code) {

          this.$Notice.success({

            title:&#39;更改日志级别为&#39;+level,

            desc:res.msg

          });

        } else {

          this.$Notice.error({

            title:&#39;更改日志级别失败&#39;,

            desc:res.msg

          });

        }

      });

 }

登入後複製

最終效果如下:

Spring Boot Actuator管理日誌如何實現

Spring Boot Actuator管理日誌如何實現

以上是Spring Boot Actuator管理日誌如何實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
java - springboot新手學習
來自於 1970-01-01 08:00:00
0
0
0
java - 新人用直接springmvc還是springboot
來自於 1970-01-01 08:00:00
0
0
0
java - springboot部署到tomcat url請求路徑問題
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板