Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting only before request logs CommonsRequestLoggingFilter in Spring Boot

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.

like image 394
leventunver Avatar asked Feb 04 '19 11:02

leventunver


People also ask

How do you log HTTP request and response in Spring boot?

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.

How do I enable information logs in Spring boot?

To enable logging in Spring, import Logger and LoggerFactory from the org. slf4j API library: import org. slf4j.

Which logger is best for Spring boot?

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.

What is default logging in Spring boot?

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.


2 Answers

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

like image 136
kladderradatsch Avatar answered Oct 19 '22 14:10

kladderradatsch


To avoid the duplicate logging, when instantiating the CommonsRequestLoggingFilter you have to override either of the two:

  • beforeRequest()
  • afterRequest()

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;
    }
}
like image 25
voccoeisuoi Avatar answered Oct 19 '22 16:10

voccoeisuoi