Request Logging

  1. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/CommonsRequestLoggingFilter.html
  2. https://www.baeldung.com/spring-http-logging

Sometimes we might have to do some additional processing, such as logging, on the HTTP request payload. Logging the incoming HTTP request is very helpful in debugging applications.

Using Spring Boot Built-In Request Logging

Spring Boot provides a built-in solution to log payloads. We can use the ready-made filters by plugging into the Spring Boot application using configuration.

AbstractRequestLoggingFilter is a filter that provides the basic functions of logging. Subclasses should override the beforeRequest() and afterRequest() methods to perform the actual logging around the request.

The Spring Boot framework provides three concrete implementation classes that we can use to log incoming requests:

  1. CommonsRequestLoggingFilter
  2. Log4jNestedDiagnosticContextFilter (deprecated)
  3. ServletContextRequestLoggingFilter

Using Servlet Filters

See https://github.com/explorer436/programming-playground/tree/main/java-playground/request-logging

Prerequisites

  1. Write a custom class that extends ServletInputStream
    1. Override the following methods
    2. isReady()
    3. read()
    4. setReadListener(ReadListener readListener)
    5. isFinished()
  2. Write a custom class that extends HttpServletRequestWrapper
    1. Override getInputStream()
      1. Use the custom ServletInputStream class here.
    2. Override getReader()
  3. Write a custom filter class extends OncePerRequestFilter
    1. Override doFilterInternal()
    2. Use the custom HttpServletRequestWrapper class here.

Logs

2025-06-04T12:29:02.170-04:00  INFO 3884347 --- [using-servlet-filters] [           main] c.e.u.UsingServletFiltersApplication     : Started UsingServletFiltersApplication in 1.318 seconds (process running for 2.055)
2025-06-04T12:29:22.287-04:00  INFO 3884347 --- [using-servlet-filters] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2025-06-04T12:29:22.287-04:00  INFO 3884347 --- [using-servlet-filters] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2025-06-04T12:29:22.288-04:00  INFO 3884347 --- [using-servlet-filters] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
2025-06-04T12:29:22.295-04:00  INFO 3884347 --- [using-servlet-filters] [nio-8080-exec-1] c.e.u.filters.RequestCachingFilter       : REQUEST DATA: {
            "isNightSurcharge" : true,
            "distanceInMile" : 10
          }

CommonsRequestLoggingFilter

See https://github.com/explorer436/programming-playground/tree/main/java-playground/request-logging

Prerequisites

  1. Create the config class
  2. Configure logback for the custom spring class

Logs

1640 [main] INFO  c.e.s.SpringCommonsRequestLoggingFilterApplication -- Started SpringCommonsRequestLoggingFilterApplication in 1.356 seconds (process running for 2.02)
1640 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean -- Application availability state LivenessState changed to CORRECT
1642 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean -- Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
11171 [http-nio-8080-exec-1] INFO  o.a.c.c.C.[Tomcat].[localhost].[/] -- Initializing Spring DispatcherServlet 'dispatcherServlet'
11171 [http-nio-8080-exec-1] INFO  o.s.web.servlet.DispatcherServlet -- Initializing Servlet 'dispatcherServlet'
11171 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- Detected StandardServletMultipartResolver
11171 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- Detected AcceptHeaderLocaleResolver
11171 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- Detected FixedThemeResolver
11171 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@652ecf71
11171 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- Detected org.springframework.web.servlet.support.SessionFlashMapManager@717b5999
11172 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
11172 [http-nio-8080-exec-1] INFO  o.s.web.servlet.DispatcherServlet -- Completed initialization in 1 ms
11177 [http-nio-8080-exec-1] DEBUG o.s.w.f.CommonsRequestLoggingFilter -- Before request [POST /taxifare/calculate/]
11179 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- POST "/taxifare/calculate/", parameters={}
11185 [http-nio-8080-exec-1] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping -- Mapped to com.example.spring_CommonsRequestLoggingFilter.controller.TaxiFareController#calculateTaxiFare(TaxiRide)
11231 [http-nio-8080-exec-1] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor -- Read "application/json;charset=UTF-8" to [com.example.spring_CommonsRequestLoggingFilter.model.TaxiRide@558a4e74]
11235 [http-nio-8080-exec-1] DEBUG c.e.s.controller.TaxiFareController -- calculateTaxiFare() - START
11235 [http-nio-8080-exec-1] DEBUG c.e.s.controller.TaxiFareController -- calculateTaxiFare() - Total Fare : 200
11235 [http-nio-8080-exec-1] DEBUG c.e.s.controller.TaxiFareController -- calculateTaxiFare() - END
11239 [http-nio-8080-exec-1] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor -- Using 'text/plain', given [*/*] and supported [text/plain, */*, application/json, application/*+json]
11239 [http-nio-8080-exec-1] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor -- Writing ["200"]
11243 [http-nio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet -- Completed 200 OK
11244 [http-nio-8080-exec-1] DEBUG o.s.w.f.CommonsRequestLoggingFilter -- REQUEST DATA: POST /taxifare/calculate/, payload={
            "isNightSurcharge" : true,
            "distanceInMile" : 10
          }]

Links to this note