修改ApiBoot Logging日誌採集的字首
ApiBoot Logging
支援指定單個或者多個路徑的字首進行採集,也就是我們可以指定/user/**
或者/order/**
下的單個或者同時指定多個路徑進行採集請求日誌,其他不符合Ant
表示式的路徑就會被忽略掉。
建立示例專案
使用idea
建立SpringBoot
專案。
新增ApiBoot Logging依賴
建立專案後在pom.xml
配置檔案內新增依賴如下所示:
<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
屬性配置類檢視原始碼。
配置採集攔截器字首
ApiBoot Logging
提供了在application.yml
配置檔案內修改的配置引數api.boot.logging.logging-path-prefix
,該配置引數接收的型別為java.lang.String[]
,所以我們可以使用,
逗號隔開配置多個路徑,如下所示:
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
的功能,如下所示:
/**
* 入口類
*
* @author 恆宇少年
*/
@SpringBootApplication
@EnableLoggingClient
public class ModifyApibootLoggingCollectionPrefixApplication {
public static void main(String[] args) {
SpringApplication.run(ModifyApibootLoggingCollectionPrefixApplication.class,args);
}
}複製程式碼
執行測試
使用idea
的Application或者java -jar xxx.jar
的形式來執行本章原始碼,本章原始碼的埠號配置為8080
,我們需要從下面幾個點進行測試。
測試點:匹配/user/**路徑
新增測試控制器類UserController
如下所示:
@RestController
@RequestMapping(value = "/user")
public class UserController {
/**
* 測試日誌攔截路徑介面
*
* @param name
* @return
*/
@GetMapping
public String welcome(@RequestParam("name") String name) {
return "hello," + name;
}
}複製程式碼
通過如下命令訪問測試介面:
➜ ~ 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
:
- Gitee:https://gitee.com/hengboy/spring-boot-chapter
作者個人 部落格
使用開源框架 ApiBoot 助你成為Api介面服務架構師