CommonsRequestLoggingFilter

CommonsRequestLoggingFilter

CommonsRequestLoggingFilter在记录请求方面相当不错,但就我而言,它在处理请求之前和之后记录的是同一件事,这是重复的和冗余的。我想摆脱请求处理后的日志,但是找不到方法。有任何想法吗?

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;
    }
}


我从here.开始

最佳答案

org.springframework.web.filter.CommonsRequestLoggingFilter只是org.springframework.web.filter.AbstractRequestLoggingFilter的子类。因此,跳过CommonsRequestLoggingFilter并编写您自己的子类,并将覆盖的方法afterRequest()留空。

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) {

    }
}

使用此类代替org.springframework.web.filter.CommonsRequestLoggingFilter

07-27 18:21