Java Spring Boot Log Response with Payloads

I am currently integrating request / response logging into a REST service using Spring Boot. For requests, I chose CommonsRequestLoggingFilter, as provided by Spring:

@Bean
public CommonsRequestLoggingFilter requestLoggingFilter() {
    CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
    loggingFilter.setIncludeClientInfo(false);
    loggingFilter.setIncludeQueryString(true);
    loggingFilter.setIncludePayload(true);
    loggingFilter.setMaxPayloadLength(1024);

    return loggingFilter;
}

      

And in the config file:

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

      

However, there is no corresponding class for the answers? Is there a similar way to log a server response in Spring?

EDIT: In particular, what I see with the above setup doesn't matter in BeforeRequest:

2017-06-28 09:32:32.258 DEBUG 22872 --- [http-nio-8081-exec-2] o.s.w.f.CommonsRequestLoggingFilter      : Before request [uri=/someApp/someObject]

      

Request payload as AfterRequest:

2017-06-28 09:32:32.272 DEBUG 22872 --- [http-nio-8081-exec-2] o.s.w.f.CommonsRequestLoggingFilter      : 
After request [uri=/someApp/someResource;payload={
  "someObject":   {
                    "lastName": "Doe",
                    "reference": "123456789"
              }
    }
]

      

And the actual answer is nowhere in the log.

+4


source to share


3 answers


Nevermind,

As a result, I created my own filter that can log responses as well. Based on GenericFilterBean, wrapping HttpResponse and HttpRequest so streams don't get closed.



I still find it strange that Spring provides a class for logging requests but not for responses ...

+2


source


If you need to monitor and control your application, consider using a drive . It has the ability to record requests / responses out of the box.



+1


source


You can try to set the following line in application.properties to log request / response

logging.level.org.springframework.ws.server.MessageTracing.sent=TRACE

      

0


source







All Articles