Write Spring MVC Logger for log request and response

Hello! In this article, I will explain how to write a logger for logging requests and responses. It would seem that there is nothing…

Write Spring MVC Logger for log request and response

Hello! In this article, I will explain how to write a logger for logging requests and responses. It would seem that there is nothing complicated about this, but if you look at the documentation, you can see the following

Which tells us that we can read data only once and by calling either the getReader method or the getInputStream method. In order to get around this limitation, two special classes were invented ContentCachingRequetWrapper.java and ContentCachingResponseWrapper.java, which are just needed so that we can log the request data. In order to polish the data about requests and responses, we will implement a filter that will extend the OncePerRequestFilter.java class Let’s call this our movie LoggingFilter and start writing it. First, let’s declare an instance of our logger

Next, we declare all the MIME types that we want to log

Then we override the methods from the parent class

In this case, we check whether the call is asynchronous, and in this case we do not try to polish it, if the call is synchronous, then we try to wrap the logging Implementing the doFilterWrapped method

In this method, we already receive a cached request and response and perform processing and post-processing of the request, and also copy the response data Let’s write the request processing code

We check the logging level and if it is equal to the INFO level, then we also log the request headers

Let’s write a post-processing method

In this case, we also check that the logging level is set to INFO before polishing the request and response. And finally, the most interesting thing, we write logging of headers, as well as requests and responses

In the logRequestHeader method, the following happens — from the request we get a string, check whether it is null and if null, then we code the HTTP method and the URL to which such a request came, otherwise the HTTP method is also logged, the URL to which the request came, and also all request headers Next, we need to write the code to log the request body

We receive data from the request as an array of bytes, and in the event that the size of the array is greater than zero, we pass the data to the logContent method, which we will write a little later.

Now is the time to write the code for logging responses from the application.

Let’s figure out what exactly is happening here — with the first line we get the HTTP response code of our application and then log data about it. Next, we go through the response headers and log them in the same way. Further, the logic is quite similar to what we have seen before — we receive data from the response as an array of bytes and then pass it to the logContent method, which will look like this:

What’s going on here? First, we check if the type of the transmitted data is supported for doping, if not, then we simply display the size of the data array. If yes, then we get create a line from the data with the specified encoding in the request, and separate each line using carriage return and newline characters. If the encoding is not supported, then, as in the case of an unsupported data type, we simply log the size of the data that we received in the request or response

And the final touch in our code is the two methods that are actually needed to wrap our HttpServletRequest and HttpServletResponse in ContentCachingRequestWrapper and ContentCachingResponseWrapper

Next, you need to register our filter in the deployment descriptor

Finally, make sure our filter is working properly

Full code of LoggingFilter.java

If you liked the article, then subscribe and share with your friends! Thanks for attention

Subscribe to Egor Voronianskii | Java Development and whatsoever

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe