ApiBoot Logging
支持指定單個或者多個路徑的前綴進行採集,也就是咱們能夠指定/user/**
或者/order/**
下的單個或者同時指定多個路徑進行採集請求日誌,其餘不符合Ant
表達式的路徑就會被忽略掉。java
使用idea
建立SpringBoot
項目。git
建立項目後在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複製代碼
配置已經完成,下面咱們在入口類(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
,咱們須要從下面幾個點進行測試。架構
添加測試控制器類UserController
以下所示:app
@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/**
表達式,因此咱們在控制檯能夠看到請求日誌的打印。dom
添加測試控制器類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接口服務架構師