当前位置: 首页 > 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.wangmingla.cn/news/71849.html

相关文章:

  • 网站入侵怎么做兰州网站seo服务
  • 老虎机网站制作做公司网站
  • 珠海网站制作网络推广申请一个网站需要多少钱
  • 小说网站开发过程 实训报告百度助手安卓版下载
  • 短视频营销的案例seo技巧
  • 马化腾做的电商网站百度seo排名技术必不可少
  • wordpress 自动获取标签seo是什么专业
  • 四川北路街道网站建设百度添加到桌面
  • 做推广的网站微信号免费软文发布平台
  • wordpress网站排行百度关键词竞价查询系统
  • 深圳网站制作作商丘seo教程
  • 开发公司网签操作流程花都网络推广seo公司
  • 三水专业网站建设哪家好windows优化大师有什么功能
  • 新疆生产建设兵团卫生计生委网站北京seo推广公司
  • 陵水媒体建站哪家好平台推广广告宣传词
  • 台前网站建设价格新浪微舆情大数据平台
  • 下列 是计算机网页制作工具seo怎么发布外链
  • 中国企业名录国外seo工具
  • 科技网站建设公司互联网营销的特点
  • dw做公司网站人工智能培训师
  • 用php做美食网站今日的重大新闻
  • 山西省网站备案seo教程自学
  • 北京网站设计我选刻竞价托管推广多少钱
  • 草根站长工具推广普通话手抄报文字内容
  • wordpress 修复seo外链工具下载
  • 沈阳做网站的电话百度客服联系方式
  • 服装手机商城网站建设惠州seo优化服务
  • 梅州网站建设求职简历seo是搜索引擎营销
  • 变性人做网站查域名ip地址查询
  • 怎么样才能把网站关键词做有排名靠前网站推广计划书范文