Modify the prefix of ApiBoot Logging log collection

Keywords: Programming Spring curl Java xml

ApiBoot Logging supports specifying the prefix of a single or multiple paths for collection, that is, we can specify a single or multiple paths under / user / * * or / order / *, and other paths that do not conform to Ant expressions will be ignored.

Create sample project

Use idea to create a spring boot project.

Add ApiBoot Logging dependency

After creating the project, add the dependency in 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>
<!--ApiBoot Version dependency-->
<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>

Default intercept path

The default interception path of API boot logging is / * *, you can visit the org.minbox.framework.api.boot.autoconfigure.logging.ApiBootLoggingProperties property configuration class to view the source code.

Configure acquisition interceptor prefix

API boot logging provides the configuration parameter api.boot.logging.logging-path-prefix modified in the application.yml configuration file. The received type of the configuration parameter is java.lang.String [], so we can use comma to separate multiple paths as follows:

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

api:
  boot:
    # Configuration of ApiBoot Logging
    logging:
      # Modify prefix of collection log
      logging-path-prefix: /user/**,/order/**
      # Console print log
      show-console-log: true
      # Beautify console printed logs
      format-console-log-json: true

Enable ApiBoot Logging Client

Configuration has been completed. Next, we add @ EnableLoggingClient annotation on the entry class (XxxApplication) or configuration class (XxxConfiguration) to enable the ApiBoot Logging function, as shown below:

/**
 * Entry class
 *
 * @author Heng Yu junior
 */
@SpringBootApplication
@EnableLoggingClient
public class ModifyApibootLoggingCollectionPrefixApplication {

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

}

Operation test

Use the Application of idea or java -jar xxx.jar to run the source code of this chapter. The port number of the source code of this chapter is 8080. We need to test from the following points.

Test point: match / user / * * path

Add the test controller class UserController as follows:

@RestController
@RequestMapping(value = "/user")
public class UserController {
    /**
     * Test log interception path interface
     *
     * @param name
     * @return
     */
    @GetMapping
    public String welcome(@RequestParam("name") String name) {
        return "hello, " + name;
    }
}

Access the test interface through the following command:

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

/The user path matches the / user / * * expression, so we can see the printing of the request log in the console.

Test point: match / 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() + ",Submit successfully.";
    }
}

Access the test interface through the following command:

➜ ~ curl -X POST http://localhost:8080/order       
Order: 24a24d24-539e-4da9-9272-e68fd592313c, submitted successfully.

/The order path matches the / order / * * expression, so we can also see the printing of the request log on the console.

Test points: 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";
    }
}

Access the test interface through the following command:

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

Because the / other path does not match the / user / *, or / order / *, we do not see the log printing in the console.

Knock on the blackboard

ApiBoot Logging supports single or multiple path configurations to filter specified path prefixes to collect logs, making log collection no longer uncontrollable and more accurate to the log collection of business requests.

Source code of this chapter

The sample source code of this article can be obtained through the following ways. The directory is SpringBoot2.x/modify-apiboot-logging-collection-prefix:

Author individual Blog Using open source framework ApiBoot Help you become Api interface service architect

Posted by brokenshadows on Mon, 28 Oct 2019 10:26:50 -0700