-1

I'm trying to log all request and response with the body for my REST service. So far for logging request, I'm using the Spring built-in solution to log payloads RequestLoggingFilterConfig and it works perfectly.

Now I'm looking for a similar solution for logs Response. The question is how can I logs the whole responses with the body from REST and can it be done only by the configuration file?

My configuration for the request

@Configuration
public class RequestLoggingFilterConfig {

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

and the application.properties

logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
lukaszgo3
  • 149
  • 3
  • 15

1 Answers1

-1

application.properties logging.level.* help just configured your logging level, For handling the logging response, You need some kind of middleware which can intercept your response

1: Custom Filter Filter is one of the best approach for handle this problem eg:

    @Component
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public class WebFilter implements Filter {

        private static final Logger logger = LoggerFactory.getLogger(WebFilter.class);

        private static final boolean CONDITION = true;

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            logger.debug("Initiating WebFilter >> ");
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {

                chain.doFilter(requestWrapper, response); 

                // log your response here
        }

        @Override
        public void destroy() {
            logger.debug("Destroying WebFilter >> ");
        }
    }

register your filter 
@Bean
public FilterRegistrationBean someFilterRegistration() {

    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(someFilter());
    registration.addUrlPatterns("/api/*");
    registration.addInitParameter("paramName", "paramValue");
    registration.setName("someFilter");
    registration.setOrder(1);
    return registration;
} 

public Filter someFilter() {
    return new WebFilter();
}


2:InterceptorAdapter

You can extend HandlerInterceptorAdapter and override the afterCompletion method public void afterCompletion,

@Component
public class LogginInterceptor 
  extends HandlerInterceptorAdapter {

    @Override
    public void afterCompletion(
      HttpServletRequest request, 
      HttpServletResponse response, 
      Object handler, 
      Exception ex) {
        // log your response here
    }
}

3:AOP Also you can use magical AOP for handle the response using @After impl

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
  • I've tried the method with afterCompletion but so far the only response i get is somethnig like `[afterCompletion] org.apache.catalina.connector.ResponseFacade@37c00390` – lukaszgo3 Mar 24 '18 at 17:45
  • HttpServletResponse does not override the toString Method thats why it's printing this response class name,If you go throw with this post, you will get how to log request/response https://stackoverflow.com/questions/33744875/spring-boot-how-to-log-all-requests-and-responses-with-exceptions-in-single-pl/39234957 – Bhushan Uniyal Mar 24 '18 at 18:11