Software engineer

How to log requests and their payloads in Spring

From time to time we may need to log our requests in order to get some information. Personally, I’m writing this short article, because we need to find out the reason why jackson throws 400 error status.

Luckily, it’s very easy to log your requests. Spring has a class AbstractRequestLoggingFilter, which has three concrete classes, which you can potentially use:

The last one is the guy we need. It’s pretty straightforward how to configure this class: just declare it in your context:

    public CommonsRequestLoggingFilter requestLoggingFilter() {
        CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
        return loggingFilter;

And yes, don’t forget to add additional line in your application.properties file:


And voila, you have your requests being logged:

2016-10-24 21:50:45.520 DEBUG 83061 --- [nio-8080-exec-3] o.s.w.f.CommonsRequestLoggingFilter      : Before request [uri=/api/requests;client=0:0:0:0:0:0:0:1]
... // My logs :)

2016-10-24 21:50:45.526 DEBUG 83061 --- [nio-8080-exec-3] o.s.w.f.CommonsRequestLoggingFilter      : After request [uri=/api/requests;client=0:0:0:0:0:0:0:1;payload={
  "title": "..."}]