CommonsRequestLoggingFilter
is quite good in logging requests but in my case, it is logging the same thing before and after the request is processed, which is duplicate and redundant. I want to get rid of the after request processing logs but I couldn't find how to do it. Any ideas?
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
@Configuration
public class RequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
filter.setIncludeClientInfo(true);
filter.setIncludeHeaders(true);
filter.setIncludePayload(false);
filter.setIncludeQueryString(true);
return filter;
}
}
I started off from here.
Custom Request Logging Among the Spring request interceptors, one of the noteworthy interfaces is HandlerInterceptor, which we can use to log the incoming request by implementing the following methods: preHandle() – we execute this method before the actual controller service method.
To enable logging in Spring, import Logger and LoggerFactory from the org. slf4j API library: import org. slf4j.
If you are using Spring Boot Starters, Logback will provide a good support for logging. Besides, Logback also provides a use of good support for Common Logging, Util Logging, Log4J, and SLF4J.
The default logging configuration in Spring Boot is a Logback implementation at the info level for logging the output to console. Let us see this behavior in action by creating a Spring Boot application. We generate a minimal application with just the web dependency using start.spring.io.
The class org.springframework.web.filter.CommonsRequestLoggingFilter is just a subclass of org.springframework.web.filter.AbstractRequestLoggingFilter. Thus skip CommonsRequestLoggingFilter and write your own subclass and leave the overwritten method afterRequest()
empty.
public class CustomizedRequestLoggingFilter extends AbstractRequestLoggingFilter {
@Override
protected void beforeRequest(HttpServletRequest httpServletRequest, String message) {
this.logger.debug(message);
}
@Override
protected void afterRequest(HttpServletRequest httpServletRequest, String message) {
}
}
Use this class instead of org.springframework.web.filter.CommonsRequestLoggingFilter
To avoid the duplicate logging, when instantiating the CommonsRequestLoggingFilter
you have to override either of the two:
To disable the logging in afterRequest(), the OP's code can be changed as follows:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
@Configuration
public class RequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter() {
@Override
public void afterRequest(HttpServletRequest request, String message) {
// No body, we are just overriding the default behavior
}
};
filter.setIncludeClientInfo(true);
filter.setIncludeHeaders(true);
filter.setIncludePayload(false);
filter.setIncludeQueryString(true);
return filter;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With