修改ApiBoot Logging日誌採集的前綴

ApiBoot Logging支持指定單個或者多個路徑的前綴進行採集,也就是咱們能夠指定/user/**或者/order/**下的單個或者同時指定多個路徑進行採集請求日誌,其餘不符合Ant表達式的路徑就會被忽略掉。java

建立示例項目

使用idea建立SpringBoot項目。git

添加ApiBoot Logging依賴

建立項目後在pom.xml配置文件內添加依賴以下所示:web

<dependencies>
  <!--Web-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--ApiBoot Logging-->
  <dependency>
    <groupId>org.minbox.framework</groupId>
    <artifactId>api-boot-starter-logging</artifactId>
  </dependency>
</dependencies>
<!--ApiBoot版本依賴-->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.minbox.framework</groupId>
      <artifactId>api-boot-dependencies</artifactId>
      <version>2.1.4.RELEASE</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

默認攔截路徑

ApiBoot Logging默認的攔截路徑是/**,能夠訪問org.minbox.framework.api.boot.autoconfigure.logging.ApiBootLoggingProperties屬性配置類查看源碼。spring

配置採集攔截器前綴

ApiBoot Logging提供了在application.yml配置文件內修改的配置參數api.boot.logging.logging-path-prefix,該配置參數接收的類型爲java.lang.String[],因此咱們能夠使用,逗號隔開配置多個路徑,以下所示:json

spring:
  application:
    name: modify-apiboot-logging-collection-prefix
server:
  port: 8080

api:
  boot:
    # ApiBoot Logging 相關配置
    logging:
      # 修改採集日誌的前綴
      logging-path-prefix: /user/**,/order/**
      # 控制檯打印日誌
      show-console-log: true
      # 美化控制檯打印的日誌
      format-console-log-json: true

啓用ApiBoot Logging Client

配置已經完成,下面咱們在入口類(XxxApplication)或者配置類(XxxConfiguration)上添加@EnableLoggingClient註解來啓用ApiBoot Logging的功能,以下所示:api

/**
 * 入口類
 *
 * @author 恆宇少年
 */
@SpringBootApplication
@EnableLoggingClient
public class ModifyApibootLoggingCollectionPrefixApplication {

    public static void main(String[] args) {
        SpringApplication.run(ModifyApibootLoggingCollectionPrefixApplication.class, args);
    }

}

運行測試

使用idea的Application或者java -jar xxx.jar的形式來運行本章源碼,本章源碼的端口號配置爲8080,咱們須要從下面幾個點進行測試。bash

測試點:匹配/user/**路徑

添加測試控制器類UserController以下所示:架構

@RestController
@RequestMapping(value = "/user")
public class UserController {
    /**
     * 測試日誌攔截路徑接口
     *
     * @param name
     * @return
     */
    @GetMapping
    public String welcome(@RequestParam("name") String name) {
        return "hello, " + name;
    }
}

經過以下命令訪問測試接口:app

➜ ~ curl http://localhost:8080/user\?name\=hengboy
hello, hengboy

/user路徑匹配/user/**表達式,因此咱們在控制檯能夠看到請求日誌的打印框架

測試點:匹配/order/**路徑

添加測試控制器類OrderController以下所示:

@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @PostMapping
    public String submit() {
        return "訂單:" + UUID.randomUUID().toString() + ",提交成功.";
    }
}

經過以下命令訪問測試接口:

➜ ~ curl -X POST http://localhost:8080/order       
訂單:24a24d24-539e-4da9-9272-e68fd592313c,提交成功.

/order路徑匹配/order/**表達式,因此咱們在控制檯也能夠看到請求日誌的打印

測試點:其餘路徑

添加測試控制器類OtherController以下所示:

@RestController
public class OtherController {

    @GetMapping(value = "/other")
    public String other() {
        return "this is other path";
    }
}

經過以下命令訪問測試接口:

➜ ~ curl http://localhost:8080/other         
this is other path

因爲/other路徑並不匹配/user/**或者/order/**表達式,因此咱們在控制檯並無看到日誌的打印

敲黑板,劃重點

ApiBoot Logging支持單個或者多個路徑配置來進行過濾指定路徑前綴來採集日誌,讓日誌採集再也不不可控,更精準的定位到業務請求的日誌採集。

本章源碼

本篇文章示例源碼能夠經過如下途徑獲取,目錄爲SpringBoot2.x/modify-apiboot-logging-collection-prefix

做者我的 博客
使用開源框架 ApiBoot 助你成爲Api接口服務架構師
相關文章
相關標籤/搜索