当前位置: 首页 > news >正文

网页设计与网站建设第05国际重大新闻

网页设计与网站建设第05,国际重大新闻,广州番禺招聘网最新招聘信息,西安模板做网站文章目录如何优雅的打印项目日志原理实现日志打印Filter注入容器如何优雅的打印项目日志 框架 springboot 原理 使用filter拦截请求,打印出请求、响应,及耗时 知识点 1、OncePerRequestFilter Filter base class that aims to guarantee a single …

文章目录

  • 如何优雅的打印项目日志
    • 原理
    • 实现
      • 日志打印Filter
      • 注入容器

如何优雅的打印项目日志

框架
springboot

原理

使用filter拦截请求,打印出请求、响应,及耗时
在这里插入图片描述

知识点
1、OncePerRequestFilter

Filter base class that aims to guarantee a single execution per request dispatch, on any servlet container.Filter base class that aims to guarantee a single execution per request dispatch, on any servlet container.
过滤器基类,旨在保证在任何 servlet 容器上每个请求调度一次执行。

兼容多种servlet版本,保证在任何 servlet 容器上每个请求调度一次执行。

2、ContentCachingRequestWrapper,ContentCachingResponseWrapper
HttpServletRequest 包装器,用于缓存从输入流和读取器读取的所有内容,并允许通过字节数组检索此内容。
源代码如下,第一次获取输入流时,复制了一份
在这里插入图片描述

因为request的数据流只能读取一次,通过过滤器读取一次后,后面的业务处理会读不到数据
通过ContentCachingRequestWrapper将请求包装,可以进行多次读取

实现

继承OncePerRequestFilter,重写doFilterInternal方法

日志打印Filter

import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;@Slf4j
public class LogFilter extends OncePerRequestFilter {private Integer printMaxSize;public LogFilter() {}public LogFilter(Integer printMaxSize) {if (printMaxSize == null) {printMaxSize = 1024;}this.printMaxSize = printMaxSize;}@Overrideprotected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {if (isAsyncDispatch(httpServletRequest)) {filterChain.doFilter(httpServletRequest, httpServletResponse);} else {doLogFilter(getRequestWrapper(httpServletRequest), getResponseWrapper(httpServletResponse), filterChain);}}private void doLogFilter(ContentCachingRequestWrapper requestWrapper, ContentCachingResponseWrapper responseWrapper, FilterChain filterChain) throws IOException, ServletException {StringBuilder sb = new StringBuilder();sb.append(System.lineSeparator()).append("Method: [").append(requestWrapper.getMethod()).append("] ").append("URI: ").append(requestWrapper.getRequestURI()).append(System.lineSeparator());long start = System.currentTimeMillis();filterChain.doFilter(requestWrapper, responseWrapper);try {sb.append(getParams(requestWrapper)).append(System.lineSeparator());sb.append(getBody(requestWrapper));sb.append(getResponse(responseWrapper)).append(System.lineSeparator());} catch (Exception e) {log.warn("日志打印失败 e:{}", Throwables.getStackTraceAsString(e));} finally {responseWrapper.copyBodyToResponse();long end = System.currentTimeMillis();sb.append("use time :").append(end - start).append("ms");log.info(sb.toString());}}private String getBody(ContentCachingRequestWrapper requestWrapper) {StringBuilder sb = new StringBuilder();String contentType = requestWrapper.getContentType();if (requestWrapper.getMethod().equalsIgnoreCase("POST")&& (MediaType.APPLICATION_JSON_VALUE.equals(contentType)|| MediaType.APPLICATION_JSON_UTF8_VALUE.equals(contentType))) {sb.append("body: ").append(System.lineSeparator());sb.append(new String(requestWrapper.getContentAsByteArray(), StandardCharsets.UTF_8));sb.append(System.lineSeparator());}return sb.toString();}private String getResponse(ContentCachingResponseWrapper responseWrapper) {String responseStr = new String(responseWrapper.getContentAsByteArray(), StandardCharsets.UTF_8);responseStr = responseStr.length() > printMaxSize ? responseStr.substring(0, printMaxSize) : responseStr;return "response: " + responseStr;}private String getParams(ContentCachingRequestWrapper requestWrapper) {Enumeration<String> enumeration = requestWrapper.getParameterNames();StringBuilder sb = new StringBuilder();sb.append("params: ");while (enumeration.hasMoreElements()) {String paramName = enumeration.nextElement();sb.append(paramName);sb.append(" : ");sb.append(requestWrapper.getParameter(paramName));sb.append(", ");}if (sb.length() > 2) {sb.replace(sb.length() - 2, sb.length(), "");}return sb.toString();}private ContentCachingResponseWrapper getResponseWrapper(HttpServletResponse httpServletResponse) {if (httpServletResponse instanceof ContentCachingResponseWrapper) {return (ContentCachingResponseWrapper) httpServletResponse;} else {return new ContentCachingResponseWrapper(httpServletResponse);}}private ContentCachingRequestWrapper getRequestWrapper(HttpServletRequest httpServletRequest) {if (httpServletRequest instanceof ContentCachingRequestWrapper) {return (ContentCachingRequestWrapper) httpServletRequest;} else {return new ContentCachingRequestWrapper(httpServletRequest, printMaxSize);}}
}

注入容器

@Configuration
public class FilterConfig {/*** 日志长度限制*/private Integer printMaxSize = 1024;@Beanpublic LogFilter getLogFilter() {return new LogFilter(printMaxSize);}}
http://www.ds6.com.cn/news/68135.html

相关文章:

  • php建站软件哪个好怎样建立一个网站
  • 做网站客户尾款老不给怎么办东莞seo培训
  • 校园网站建设教程比较好的网络优化公司
  • 渝北区两江新区seo网站推广软件
  • 门户网站等保二级建设方案seo教程seo入门讲解
  • 有个专门做简历的网站叫seo领导屋
  • 国内外贸网站建设公司百度网页版网址
  • aide web长春seo公司哪家好
  • h5网站全球搜官网
  • 莲都区建设局门户网站互联网平台推广
  • wordpress 媒体库空白上海排名优化seo
  • 天猫店的网站怎么做的sem是什么显微镜
  • 医院网站建设方案计划书google play应用商店
  • 电子商务网站的建设报告市场营销推广策划方案
  • 杭州高端网站建设搜索引擎营销的案例
  • wordpress注册登录问题网站seo快速排名优化的软件
  • 怎么做网站首页关键词深圳百度推广竞价托管
  • 网站后台的关键词福州关键词排名软件
  • 网站建设与设计实验报告市场调研报告范文模板
  • 网站网页能自己做吗网站排名seo教程
  • app开发定制外包22宁阳网站seo推广
  • 网站读取错误时怎样做网络营销策略优化
  • 哪个网站教做饭做的好交换友情链接的意义是什么
  • 网站申请免费培训心得模板
  • 微信网站建设热线免费公司网站建站
  • 做网站的成本在哪广州营销课程培训班
  • 国内ui做的好的网站有哪些如何做好网络销售技巧
  • 有没有教做黄色网站厦门seo推广优化
  • 亚运会110周年庆典在杭州举行长沙网站优化方法
  • 苏州企业网站建设专家站点查询