10

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.

Jan-Eric Pietralla
  • 121
  • 1
  • 1
  • 5

3 Answers3

2

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...

Jan-Eric Pietralla
  • 121
  • 1
  • 1
  • 5
  • 3
    It will be quite useful, if you also post the code for the LoggingFilter you implemented. – Pratik Singhal Nov 18 '17 at 08:23
  • @PratikSinghal example using (extending) Filters to log response along with the body: https://stackoverflow.com/a/3242482/885922 – xlm Sep 28 '18 at 05:41
1

If you need to monitor and manage your app consider to use actuator. It has ability to log requests\responses out of the box.

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • I see. Any pointers in getting this activated properly? I currently see the request with payload logged as "AfterRequest" entry, and the response is nowhere to be found. – Jan-Eric Pietralla Jun 28 '17 at 07:34
  • AbstractRequestLoggingFilter can log the **request** before and after processing, but does not log the response. – pnewhook Apr 30 '18 at 19:01
-1

you can try to set the below line in application.properties to log request/response

logging.level.org.springframework.ws.server.MessageTracing.sent=TRACE
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Amr Ahmed
  • 1
  • 1