Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring Boot - logging response with payloads

I'm currently integrating request / response logging into a REST service using Spring Boot. For requests, I chose the 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 configuration file:

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

For reponses, however, there seems to be no corresponding class? Is there a similar way to log the server response in Spring?

EDIT: Specically, what I see with the above setup is nothing 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]

The 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 response is nowhere in the log.

like image 767
Jan-Eric Pietralla Avatar asked Jun 27 '17 09:06

Jan-Eric Pietralla


1 Answers

Nevermind,

I ended up implementing my own filter that can log responses as well. Based on GenericFilterBean, wrapping HttpResponse and HttpRequest so the streams don't get closed.

It's still strange to me that Spring provides a class for logging requests, but not for responses...

like image 155
Jan-Eric Pietralla Avatar answered Oct 04 '22 12:10

Jan-Eric Pietralla