12

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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

It will be quite useful, if you also post the code for the LoggingFilter you implemented.
@PratikSinghal example using (extending) Filters to log response along with the body: stackoverflow.com/a/3242482/885922
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.

2 Comments

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.
AbstractRequestLoggingFilter can log the request before and after processing, but does not log the response.
-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.