zoukankan      html  css  js  c++  java
  • 日志打印request 和response 内容

    package com.mytian.dueros.filter;
    
    import com.alibaba.fastjson.JSON;
    import org.apache.catalina.connector.ResponseFacade;
    import org.apache.logging.log4j.ThreadContext;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import com.alibaba.fastjson.JSON;
    import com.github.isrsal.logging.RequestWrapper;
    import com.github.isrsal.logging.ResponseWrapper;
    import org.apache.catalina.connector.RequestFacade;
    import org.apache.catalina.connector.ResponseFacade;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.logging.log4j.ThreadContext;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletResponseWrapper;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    /**
     * @author m.feng
     * @create 2019-01-03-14:31
     */
    @WebFilter(filterName="logFilter",urlPatterns="/*")
    public class LogFilter implements Filter {
        private static final Logger log = LoggerFactory.getLogger(LogFilter.class);
    
        private static final String ignoreUrlRegex = ".*((pay/)|(/index)|(/index/.*)|([.]((html)|(jsp)|(css)|(js)|(gif)|(png))))$";
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            ThreadContext.put("TId", UUID.randomUUID().toString());
            ResponseWrapper  responseWrapper = new ResponseWrapper (Thread.currentThread().getId(), (HttpServletResponse) servletResponse);
            HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
            RequestWrapper requestWrapper = new RequestWrapper(Thread.currentThread().getId(), httpServletRequest);
    
            // 请求html页面、js不打印日志
            if (httpServletRequest.getRequestURI().matches(ignoreUrlRegex)) {
                ThreadContext.clearAll();
                filterChain.doFilter(servletRequest, responseWrapper);
                return;
            }
    
            Map params;
            // 记录入参
            log.info("请求的URL:" + httpServletRequest.getRequestURI());
    
            filterChain.doFilter(requestWrapper, responseWrapper);
    
            // 打印from格式的入参信息
            params = servletRequest.getParameterMap();
            if (null != params && params.size() != 0) {
                log.info("入参:" + JSON.toJSONString(params));
            } else {
                // 打印json格式的入参信息
                String charEncoding = requestWrapper.getCharacterEncoding() != null ? requestWrapper.getCharacterEncoding() : "UTF-8";
                log.info("入参" + new String(requestWrapper.toByteArray(), charEncoding));
            }
    
            // 记录出参
            String outParam = new String();
            // 记录出参响应头
            params = new HashMap();
            // 如果响应头存在errorCode则打印,除文件下载外均不存在
            try {
                params.put("errorCode", ((ResponseFacade) servletResponse).getHeader("errorCode"));
                params.put("errorMsg", (URLDecoder.decode(((ResponseFacade) servletResponse).getHeader("errorMsg"), "UTF-8")));
                outParam = JSON.toJSONString(params);
            } catch (Exception e) {
            }
    
            // 记录出参响应体
            if (params.size() < 2) {
                outParam = outParam + new String(responseWrapper.toByteArray(), responseWrapper.getCharacterEncoding());
            }
    
            log.info("出参:" + outParam);
    
            ThreadContext.clearAll();
        }
    
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    
        @Override
        public void destroy() {
        }
    }
    

      引入依赖

    <dependency>
                <groupId>com.github.isrsal</groupId>
                <artifactId>spring-mvc-logger</artifactId>
                <version>0.2</version>
                <exclusions>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    自己留着做个记录,引用老哥博文,需要删除说一声

    https://www.cnblogs.com/chenhao0302/p/9186346.html

  • 相关阅读:
    Send EMail from your .NET Application using your GMail Account
    ObjectContext 是开发人员在查询、添加和删除其实体实例以及将新状态保存回数据库时用到的主要构造
    AjaxPro使用说明
    .NET Framework 类库
    sysobjects syscolumns和SysTypes笔记
    javascript 懒加载技术(lazyload)简单实现
    WEB前端开发笔试题2
    css方框模型(盒模型Box Model)
    让IE支持CSS3选择器的方法(利用JS)
    让IE6/IE7/IE8浏览器支持CSS3属性
  • 原文地址:https://www.cnblogs.com/mfser/p/10214173.html
Copyright © 2011-2022 走看看