Request log is almost a necessary module for all large enterprise projects. Request log is used for troubleshooting, request diversion processing, traffic limitation and so on in the later period when the project runs online. Request logs generally record request parameters, request address, request Status Code, SessionId, request Method, request time, client IP address, request return content, time, and so on. If your system has other personalized configurations, you can also complete the record.
Objective in this chapter
Request logs are recorded through SpringBoot integration and interceptor integration. This section uses SpringDataJPA and MySQL database to record logs.
Build the project
We used InteiiJ IDEA to create a SpringBoot project with pre-dependent modules such as Web, JPA, MySQL, Druid, etc. The project structure is shown in Figure 1 below:
Configuring a Data Source
We directly copy an application.yml file from the previous project into /resource. The application.yml content is shown in Figure 2 below:
Create the data table structure
Create a request log table, as shown in Figure 3:
There are a lot of fields in our table structure. After the data structure is created, create entities and configure entity JPA according to the table structure, as shown in Figure 4 below:
As you can see in Figure 4, we create entity mappings and field mappings for table and field names in the database. We create a subinterface (LoggerJPA) that implements SpringDataJPA and JpaRepository, as shown in Figure 5 below:
The JpaRepository interface contains the CRUD methods commonly used in SpringDataJPA, which we will branch out in later sections.
Create a log interceptor
Now that we have written the above steps for storing the request log, we need to write an interceptor for the request log. The custom SpringMVC interceptor needs to implement the HandlerIntercptor interface and implement the three internal methods, as shown in Figure 6 below:
The above three methods are described in the previous chapter: Chapter 6: How to use interceptors in SpringBoot projects, so I don’t have to explain them here, but please refer to chapter 6 if you need to. One thing to note here is that we can’t inject LoggerJPA in the interceptor as a SpringBean, I can only inject it in another form.
WebApplicationContextUtils
This utility class retrieves spring-managed beans from the context of the HttpServletRequest request object (ServetCotext), as shown in Figure 7 below:
As you can see in Figure 7, we create a getDAO method that takes an entity type and an HttpServetRequest request object, Through internal WebApplicationContextUtils getRequiredWebApplicationContext method to obtain the BeanFactory (entity factory class), The factory entity’s getBean method will get the LoggerJPA persistent data interface instance that SpringDataJPA manages for us.
Logging requests
We need to use FastJson and HttpServet dependencies when processing log requests, so we modify the PM. XML configuration file to add the FastJson open source component and maven dependencies for HttpServlet, as shown in Figure 8 below:
Next we write the request log creation. First we create the LoggerEntity entity in the preHandle method and write the necessary parameters to the current request object HttpServletRequest, as shown in Figure 9 and figure 10 below:
You can see that we record most of the parameters in our logging entity, when the user sends a request it goes into the preHandle method before it goes into the SpringMVC controller, and then we log the content of our request log and write the entity of the request log into the request object, Now we will enter the methods corresponding to the springMVC controller method, and execute the afterCompletion method we need to write next as the final render view is about to return to the foreground, as shown in Figure 11 below:
In the afterCompletion method, we record the request code, request timestamp, request return value, and so on. Where do we set the return value of the request? So let’s start writing the test request controller IndexController. You’ll see.
Write test controller
We create an IndexController under the Controller package and add the @RestController annotation to indicate that IndexController is a restful controller. As shown in Figure 12 below:
We simply added a test method login to the IndexControll controller, and you can see the explanation of our query in the interceptor, LOGGER_RETURN is passed from our request method into the afterCompletion method inside the LoggerInterceptor interceptor.
Configuring interceptors
Our code is almost complete. Don’t forget the most important step, we need to add the LoggerInterceptor interceptor to our SpringBoot project so that the SpringBoot project can recognize intercepts. We need to create a LoggerConfguration configuration class, as shown in Figure 13 below:
Now that our project coding is complete, let’s try running the project to see what happens.
Initial attempt to run the project
The run log is shown in Figure 14 below. If no exception occurs, the project is successfully run. If any exception occurs, please check the error according to the corresponding exception.
Our program has been running successfully, so we can test our request log record of success, we first to access the address: http://127.0.0.1:8080/index/login? Name =yuqiyu (insert link of brief book cannot have parameters, please copy to address bar for access), the effect is as shown in Figure 15 below:
See that we have successfully returned the JSON information configured in the Login method of IndexControll. Has my request log been logged to the database? Let’s first see if the InteiiJ IDEA tool’s controller has printed SQL, as shown in Figure 16 below:
It can be seen that it has been printed normally. Let’s open the table and check the data, as shown in Figure 17 below:
We have successfully written the request log to the database.
conclusion
This is the end of this chapter, which explains how the SpringBoot project can configure the log interceptor, write user request parameters to the database, and persist data using the SpringDataJPA and Druid connection pool. If you are currently using it in large enterprise projects, clean up the request logs periodically. If you need to log error messages, you can do this by checking the statusCode of the HttpServertReponse object. The specific error stack information will be described in the following sections.
This chapter has been updated to the code cloud
SpringBoot source code address: gitee.com/hengboy/spr…
SpringCloud source code address: gitee.com/hengboy/spr…
For SpringBoot related articles, visit: Directory: SpringBoot learning directory
For a series of QueryDSL articles, visit the QueryDSL Generic Query Framework learning directory
For articles in the SpringDataJPA series, visit: Directory: SpringDataJPA Learning directory
SpringBoot related articles please visit: directory: SpringBoot learning directory, thank you for reading!
Welcome to join QQ technical exchange group, common progress.