Logging request & response in SpringBoot using Groovy

Views: 51   

It will be extremely useful to debug issues if you can log request and response objects in your spring boot applications. This post shows you how can you achieve that using a filter in groovy.

Spring boot comes with CommonsRequestLoggingFilter which has beforeRequest and afterRequest methods which logs the request & responses. But by default, the log statements are in debug mode. So to enable the request and response logging you need to so the following two steps.

1. Enable CommonsRequestLoggingFilter

Add the following code to you Main class where you are starting your Spring Boot application.

 @Bean
 public Filter requestLoggingFilter() {
  CommonsRequestLoggingFilter crlf = new CommonsRequestLoggingFilter();
  crlf.setIncludeClientInfo(true);
  crlf.setIncludeQueryString(true);
  crlf.setIncludePayload(true);
  crlf.setMaxPayloadLength(100000);
  return crlf;
 }

2.Enable debug level logging in application.properties file

Add the following line to your application.properties file

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

That's these configurations will start logging all your request and responses into the log file.

On By