Home > Spring > Spring Boot > Spring Boot REST API – Log Incoming HTTP Requests

Spring Boot REST API – Log Incoming HTTP Requests

For our spring boot RESTFul API application, it becomes essential to log all the API requests and responses to our debugging purpose. We came across lots of different approaches to solve the logging problem. We will discuss here CommonsRequestLoggingFilter which is a spring built-in request logging solution, which we can configure by just adding one bean and enabling some of its properties. The drawback of this approach is that in this way we can only log the incoming request and not the responses of that request. But if the logging of both request and response is required, then we have to go for another custom logging approach, which I have written in my article Spring Boot REST API – Log Incoming HTTP Requests and Responses.

Lets learn here how to log incoming HTTP requests in Spring Boot REST API application.

We assume here you already have one Spring Boot REST API application running. So we will directly move to the logging integration part.

  1. CustomRequestLoggingFiler – Spring Built-In Request Logging class

Create a CustomRequestLoggingFiler configuration class and declare a Bean which defines the request logging solution by –

  • enabling the ClientInfo (like IP and other details)
  • enabling the query string logging
  • enabling the payload details of the API
  • setting the maximum length of the payload allowed for logging
  • disabling the header information of the request, which was not required in my application, but you can decide for your application.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

@Configuration
public class CustomRequestLoggingFiler {
    @Bean
    public CommonsRequestLoggingFilter requestLoggingFilter() {
        CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
        loggingFilter.setIncludeClientInfo(true);
        loggingFilter.setIncludeQueryString(true);
        loggingFilter.setIncludePayload(true);
        loggingFilter.setIncludeHeaders(false);
        loggingFilter.setMaxPayloadLength(64000);
        return loggingFilter;
    }
}

2. This logging approach requires you to set the log level to DEBUG. This we can do by adding the below line in our application.properties file.

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG

That’s it. Now when you will call your application API’s the request details will be logged in the log file defined, something like the below.

GET Request Log

2021-05-20 20:55:58.101 DEBUG 80095 --- [http-nio-8080-exec-1] o.s.w.f.CommonsRequestLoggingFilter      : After request [GET /get-by-id/153438, client=0:0:0:0:0:0:0:1]

POST Request Log

2021-05-21 18:04:06.631 DEBUG 96096 --- [http-nio-8080-exec-1] o.s.w.f.CommonsRequestLoggingFilter      : After request [POST /create, client=0:0:0:0:0:0:0:1, payload={
  "name": "jkoder",
  "status": 1,
}]