ApiBoot Logging integrates Minbox-logging to manage log information of each request, including header information, parameters, main content, path, and related information of the server where the request occurs. According to the response status of the interface, ApiBoot Logging can also record the response header information, response content, and stack information when an exception occurs.

Minbox – Projects Open Source Organization

Org.minbox.framework is dedicated to providing developers with a series of “out-of-the-box” framework implementation solutions.

Since the implementation of the ApiBoot framework, the increasing number of internally integrated third-party plugins has also led to the excessively redundant ApiBoot source code. To address this problem, minbox-projects open source organization was born, and ApiBoot was the first one to join this organization. In addition, the third-party plug-ins integrated in ApiBoot will be separated one after another, and each plug-in will be added to minbox-Projects as an independent open source project, so as to facilitate the separate maintenance and update of each project.

Organization home page: gitee.com/minbox-proj…

Minbox-logging Logging component

The Minbox-Logging component, part of the Open source organization Minbox-Projects, is a distributed, zero-intrusive, link-request logging analysis framework.

The Admin endpoint is used for log collection, log analysis, log alarm notification, and service performance analysis. Using the Admin Ui, you can view real-time link logs and online service service list, and analyze and record request -> Response service logs.

Minbox-logging: gitee.com/minbox-proj…

Creating a sample project

Create a SpringBoot project using the IDEA development tool.

  • Pom. XML dependency
<! -- Configure parameters -->
<properties>
  <java.version>1.8</java.version>
  <api.boot.version>2.1.4. The RELEASE</api.boot.version>
</properties>

<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>

<dependencyManagement>
  <! ApiBoot unified version dependency -->
  <dependencies>
    <dependency>
      <groupId>org.minbox.framework</groupId>
      <artifactId>api-boot-dependencies</artifactId>
      <type>pom</type>
      <scope>import</scope>
      <version>${api.boot.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>
Copy the code
  • Test controller

Add a LoggingSampleController controller for testing, source code as follows:

/** * Request log example **@authorHeng Yu Teenager */
@RestController
@RequestMapping(value = "/test")
public class LoggingSampleController {
    /** * Validates the request parameters and the corresponding contents **@param name
     * @return* /
    @GetMapping
    public String hello(@RequestParam("name") String name) {
        return "Hello." + name;
    }

    /** * validates the body request content and the corresponding content **@param user
     * @return* /
    @PostMapping
    public String bodyHello(@RequestBody User user) {
        return "Hello." + user.getName();
    }

    /** * RequestBody sample class */
    @Data
    public static class User {
        privateString name; }}Copy the code
  • application.yml
spring:
  application:
    name: apiboot-unified-manage-request-logs
server:
  port: 8080
Copy the code

Since ApiBoot Logging requires Logging of server-related information, the spring.application.name and server.port parameters must be configured, otherwise an error message will be thrown when the project is started.

  • @ EnableLoggingClient annotations
@SpringBootApplication
@EnableLoggingClient
public class ApibootUnifiedManageRequestLogsApplication {

    public static void main(String[] args) { SpringApplication.run(ApibootUnifiedManageRequestLogsApplication.class, args); }}Copy the code

Using the @ EnableLoggingClient annotations to open the log of the client, the annotation configuration on the entry class, through internal ImportBeanDefinitionRegistrar register minbox – logging – Bean client need.

Versions of ApiBoot are uniformly dependent

When we used SpringBoot, we found that we didn’t need to specify a specific version number for the dependencies we added. This is where version-uniform dependencies come in, mostly due to Maven inheritance.

In ApiBoot, the module api-boot-Dependencies is used to configure the dependencies of each third party or built-in dependencies.

We did this by adding the following version management dependencies in the POM.xml configuration file in the project:

<dependencyManagement>
  <! ApiBoot unified version dependency -->
  <dependencies>
    <dependency>
      <groupId>org.minbox.framework</groupId>
      <artifactId>api-boot-dependencies</artifactId>
      <type>pom</type>
      <scope>import</scope>
      <version>${api.boot.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>
Copy the code

You can use all the dependencies provided by ApiBoot without specifying a version number.

The test request

The following curl command is used to access the test interface:

curl http://localhost:8080/test\? name\=hengboyCopy the code

After the access is complete, the request is successful, but the console does not print any request logs, but a warning log:

Not set【 LoggingAdminDiscovery 】inLoggingFactoryBean, don't invoke report request logs.
Copy the code

This warning clearly tells us that we are not configured with logging-admin, so log reporting cannot be performed. We are using the ApiBoot logging component independently in this section, so this warning can be ignored.

The console prints request logs

ApiBoot Logging provides a configuration api.boot.logging.show-console-log, which defaults to false, to print request logs on the console.

Add the configuration to the application.yml configuration file as follows:

api:
  boot:
    ApiBoot Logging Logging component configuration
    logging:
      show-console-log: true
Copy the code

After adding, restart the project and access the test interface again. The console prints the following:

The 10:20:18 2019-10-16. 3930-489 the INFO [task - 1] O.M.F.L.C.N.S upport. LoggingLocalNotice: Request Uri: /test, Logging:  {"endTime":1571192418416,"httpStatus":200,"requestBody":"","requestHeaders":{"host":"localhost:8080","user-agent":"curl / 7.64.1 ", "accept" : "* / *"}, "requestIp" : "0:0:0:0:0:0:1-0," "requestMethod" : "GET", "requestParam" : "{\" name \ ": \" hengboy \ "} ", "re ResponseBody questUri ":"/test ", "" :" hello: Hengboy responseHeaders ", "" : {}," serviceId ":" apiboot - unified - manage - request - logs, "" serviceIp 127.0.0.1" : ""," servicePort ":" 8 080","spanId":"35a22772-5015-438a-a441-ba407926b789","startTime":1571192418391,"timeConsuming":25,"traceId":"ec53d162-31 4e-4516-8c24-5d5e03181543"}Copy the code

At this point we can see the printed request log information, but the printed log content is not beautified. Don’t worry, ApiBoot Logging also provides a configuration to beautify the output.

The console embellishes the request log

ApiBoot Logging provides the configuration api.boot.logging.format-console-log-json. This parameter is false by default and can be modified to beauty-print request logs.

Add the configuration to the application.yml configuration file as follows:

api:
  boot:
    ApiBoot Logging Logging component configuration
    logging:
      show-console-log: true
      format-console-log-json: true
Copy the code

After adding, we restart the project again, access the test interface, and the console prints as follows:

The 10:24:05 2019-10-16. 4051-480 the INFO [task - 1] O.M.F.L.C.N.S upport. LoggingLocalNotice: Request Uri: /test, Logging:  { "endTime":1571192645404, "httpStatus":200, "requestBody":"", "requestHeaders":{ "accept":"*/*", "The host" : "localhost: 8080", "the user-agent" : "curl / 7.64.1"}, "requestIp" : "0:0:0:0:0:0:1-0," "requestMethod" : "GET", "RequestParam" : "{\" name \ ": \" hengboy \ "} ", "requestUri" : "/ test", "responseBody" : "hello: Hengboy responseHeaders ", "" : {}," serviceId ":" apiboot - unified - manage - request - logs, "" serviceIp 127.0.0.1" : "", "servicePort":"8080", "spanId":"277c0973-8042-4740-a8e7-2dbb0c7bb42c", "startTime":1571192645381, "timeConsuming":23, "traceId":"7a742942-f3cc-4d72-9493-d828b090f1cc" }Copy the code

Is it straightforward to see the details of the request? However, you are advised to configure the log according to your project’s actual situation. The beautified log will occupy more console lines.

LoggingNotice Indicates log notification

ApiBoot Logging provides an interface for Logging notifications. This interface allows you to retrieve log objects for each request and customize the execution order of each Logging notification implementation class.

The implementation classes provided within ApiBoot Logging are shown below:

  • LoggingLocalNotice

    This class is used to print the request log on the console and beautify the request log. The priority is Ordered#HIGHEST_PRECEDENCE.

  • LoggingAdminNotice

    This class is used to report request logs to Logging Admin. The precedence of this class is as follows: Ordered#HIGHEST_PRECEDENCE +1, lower than LoggingLocalNotice.

Add headers using LoggingNotice

Now that we know about the two built-in LoggingNotice implementation classes, what if we add a custom LoggingNotice implementation class to add our own custom header to the RequestHeader that requests the log?

The AddHeaderLoggingNotice notification class is as follows:

/** * pass {@linkLoggingNotice} Adds area information * * to the request header of the log@authorHeng Yu Teenager */
@Component
public class AddHeaderLoggingNotice implements LoggingNotice {
    /** * Area header key */
    private static final String SERVER_REGION = "server-region";

    @Override
    public void notice(MinBoxLog minBoxLog) {
        minBoxLog.getRequestHeaders().put(SERVER_REGION, "JiNan");
    }

    /** * Maximum priority **@return* /
    @Override
    public int getOrder(a) {
        returnHIGHEST_PRECEDENCE; }}Copy the code

Since minbox-logging was designed with this in mind, it is relatively easy to add. We just need to adjust the priority of our custom log notifications and then change the value of the request log object using the #notice method.

Type on the blackboard and underline

ApiBoot this section, we introduced the integrated use of Logging, request logs can be used for acquisition, ability is not only that, used properly it can be very powerful, log to inform design can make us very good control of a request log, to add the Logging identification, classification, etc., can be configured to control the log print and beautification.

Author’s personal blog uses the open source framework ApiBoot to help you become an Api service architect