添加filter过滤器,打印日志

Posted 88aa123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加filter过滤器,打印日志相关的知识,希望对你有一定的参考价值。

filter类

技术分享图片
package com.xiaoping.house.filter;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;

public class LogFilter implements Filter {
    private Logger logger= LoggerFactory.getLogger(getClass());
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("Request--comming");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
View Code

注册filter

技术分享图片
package com.xiaoping.house.filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

@Configuration
public class FilterBeanConfig {
    @Bean
    public FilterRegistrationBean logFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new LogFilter());
        ArrayList<String> urlList = new ArrayList<>();
        urlList.add("*");
        filterRegistrationBean.setUrlPatterns(urlList);

        return filterRegistrationBean;
    }
}
View Code

 

以上是关于添加filter过滤器,打印日志的主要内容,如果未能解决你的问题,请参考以下文章

argparse 代码片段只打印部分日志

logcat

filter,servlet,listener,interceptor

springboot中如何在切面中重写请求参数

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

Logback中如何自定义灵活的日志过滤规则