Reputation: 3399
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.
Upvotes: 12
Views: 7610
Reputation: 337
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;
}
}
Upvotes: 5
Reputation: 818
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
Upvotes: 9