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接口服务架构师