5

I am using Spring Boot and REST services using @RestController. I want to log all requests and responses with a payload. How can I do this using Spring Boot Actuator? When I using:

    @Bean
    public ServletContextRequestLoggingFilter requestLoggingFilter() {
        ServletContextRequestLoggingFilter loggingFilter = new ServletContextRequestLoggingFilter();
        loggingFilter.setIncludeClientInfo(true);
        loggingFilter.setIncludeQueryString(true);
        loggingFilter.setIncludePayload(true);
        loggingFilter.setIncludeHeaders(true);
        loggingFilter.setMaxPayloadLength(10000);
        loggingFilter.setAfterMessagePrefix("REQUEST DATA : ");
        return loggingFilter;
    }

I get only Requests, but not Responses.

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
All_Safe
  • 1,339
  • 2
  • 23
  • 43
  • Possible duplicate of [Spring Boot - How to log all requests and responses with exceptions in single place?](https://stackoverflow.com/questions/33744875/spring-boot-how-to-log-all-requests-and-responses-with-exceptions-in-single-pl) – lakshman Oct 22 '18 at 10:26

2 Answers2

1

See baeldung Spring – Log Incoming Requests guide, specifically the 5th section Spring Built-In Request Logging

TL;DR If you are using Spring Boot you can configure that easily like this:

@Configuration
public class RequestLoggingFilterConfig {

    @Bean
    public CommonsRequestLoggingFilter logFilter() {
        CommonsRequestLoggingFilter filter
          = new CommonsRequestLoggingFilter();
        filter.setIncludeQueryString(true);
        filter.setIncludePayload(true);
        filter.setMaxPayloadLength(10000);
        filter.setIncludeHeaders(false);
        filter.setAfterMessagePrefix("REQUEST DATA : ");
        return filter;
    }
}

General note: In production application you should be careful logging payloads as they may contain private user data that should not be logged from privacy considerations.

Orr Benyamini
  • 395
  • 2
  • 9
-1

You can use addInterceptors method of WebMvcConfigurer

@Configuration
@EnableWebMvc
class WebMvcConfiguration : WebMvcConfigurer {

  override fun addInterceptors(registry: InterceptorRegistry) {
      registry.addInterceptor(LoggingInterceptor())
  }
}

And here is good example for interceptor.

UPDATED

You can override the Spring LoggingFilter

public class CustomLoggingFilter extends LoggingFilter
{
     @Override
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
     {
          super.doFilterInternal(request, response, filterChain);
     }
}

@Configuration
public Config {
    @Bean
    public CustomLoggingFilter loggingFilter()
    {
        return new CustomLoggingFilter();
    }
}

Not sure about your format, but you can try to use:

logging:
  level: 
    org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor: debug //response
    org.apache.coyote.http11.Http11InputBuffer: debug //request
Ihar Sadounikau
  • 741
  • 6
  • 20
  • I try like this: https://www.baeldung.com/spring-http-logging But It dont work for me, payload don't logging! – All_Safe Oct 22 '18 at 10:23
  • And your example don't have logging of payload. Without payload It make easy! – All_Safe Oct 22 '18 at 10:28
  • I saw it. It use `DispatcherServlet`, but I using `Spring Boot` – All_Safe Oct 22 '18 at 10:46
  • When providing an answer please make sure it answers the requirements in the question and not similar (but different) requirements (your solution does not log payload, and the provided link does not use Spring Boot) – Orr Benyamini Jan 29 '22 at 20:33