ApiBoot Logging allows you to specify a single path or multiple paths to collect the request log. That is, you can specify a single path or multiple paths under /user/** or /order/** to collect the request log. Other paths that do not match the Ant expression are ignored.
Creating a sample project
Create a SpringBoot project using IDEA.
Add the ApiBoot Logging dependency
After creating the project, add the dependencies to the POM.xml configuration file as follows:
<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> <! --> <dependencies> <dependency> <groupId>org.minbox.framework</groupId> < artifactId > API - the boot - dependencies < / artifactId > < version > 2.1.4. RELEASE < / version > < scope > import < / scope > < type > pom < / type > </dependency> </dependencies> </dependencyManagement>Copy the code
Default interception path
ApiBoot Logging block the default path is / * *, can access org. Minbox. Framework. The API. The boot. Autoconfigure. Logging. ApiBootLoggingProperties attribute configuration class for the source code.
Configure the collection interceptor prefix
ApiBoot Logging provides the configuration parameter api.boot.logging.logging-path-prefix that is modified in the application.yml configuration file and receives the type java.lang.string [], So we can configure multiple paths, separated by commas, as follows:
spring: application: name: modify-apiboot-logging-collection-prefix server: port: 8080 api: boot: Logging: /user/**,/order/** Format-console-log-json: trueCopy the code
Enable ApiBoot Logging Client
Now that the configuration is complete, add @enableloggingClient annotation to the entry class (XxxApplication) or configuration class (XxxConfiguration) to enable ApiBoot Logging, as shown below:
/** * @author */ @springBootApplication @enablelogGingClient public class ModifyApibootLoggingCollectionPrefixApplication { public static void main(String[] args) { SpringApplication.run(ModifyApibootLoggingCollectionPrefixApplication.class, args); }}Copy the code
Run the test
Use idea Application or Java-jar XXx. jar form to run this chapter source code, this chapter source port number configuration for 8080, we need to test from the following points.
Test point: match the /user/** path
Add the test controller class UserController as follows:
@restController@requestMapping (value = "/user") Public class UserController {/** * Test log path interface ** @param name * @return */ @GetMapping public String welcome(@RequestParam("name") String name) { return "hello, " + name; }}Copy the code
Run the following command to access the test interface:
➜ ~ curl http://localhost:8080/user\? name\=hengboy hello, hengboyCopy the code
The /user path matches the /user/** expression, so we can see the request log printed on the console.
Test point: Match the /order/** path
Add the test controller class OrderController as follows:
@RestController @RequestMapping(value = "/order") public class OrderController { @PostMapping public String submit() { Return "order:" + uuid.randomuuid ().toString() + ", commit successfully."; }}Copy the code
Run the following command to access the test interface:
➜ ~ curl -x POST http://localhost:8080/order orders: 24 a24d24-539-4 da9-9272 - e e68fd592313c, submitted to success.Copy the code
The /order path matches the /order/** expression, so we can also see the request log printed on the console.
Test point: other paths
Add the test controller class OtherController as follows:
@RestController public class OtherController { @GetMapping(value = "/other") public String other() { return "this is other path"; }}Copy the code
Run the following command to access the test interface:
➜ ~ curl http://localhost:8080/other
this is other pathCopy the code
Since the /other path does not match the /user/** or /order/** expressions, we do not see the log printed on the console.
Type on the blackboard and underline
ApiBoot Logging supports single or multiple path configurations to filter specified path prefixes to collect logs, so that log collection is not uncontrollable and more accurate to locate the log collection of service requests.
This chapter source
The sample source code for this article can be obtained from the following directory: springboot2. x/modify-apiboot-logging-collection-prefix:
- Gitee:https://gitee.com/hengboy/spring-boot-chapter
Author’s Personal blog
Use the open source framework ApiBoot to help you become an Api service architect