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

网站建设视频教程网站网站开发的关键

网站建设视频教程网站,网站开发的关键,网站开发人员需要什么要求,聚美优品网站建设文章目录如何优雅的打印项目日志原理实现日志打印Filter注入容器如何优雅的打印项目日志 框架 springboot 原理 使用filter拦截请求#xff0c;打印出请求、响应#xff0c;及耗时 知识点 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、ContentCachingRequestWrapperContentCachingResponseWrapper 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) {EnumerationString 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.hkea.cn/news/14261959/

相关文章:

  • 无锡鸿源建设集团有限公司网站石家庄做网站备案有哪些公司
  • 鄢陵网站建设电脑建站专业制作网站用哪些软件
  • 怎么做公益网站1688黄页大全进口
  • 如东做网站南京做网站优化的企业排名
  • 做外贸需要英文网站南京网络营销服务
  • 建设英文网站的必要性做一个微信小程序游戏需要多少钱
  • 都有哪些js素材网站企业网站的建设与流程
  • h5网站源码如何用wordpress挖比特币
  • 建设个人网站的参考网站及文献2018wordpress极品主题
  • 网站备案查询不出来石家庄公司网站设计
  • 网站导航栏效果分类目录 代码 wordpress
  • 深圳龙华医院网站建设wordpress网站内容
  • 烟台哪家公司可以做网站南宁江南区网站制作多少钱
  • 找装修公司去哪个网站推广普通话奋进新征程演讲稿
  • 网站开发按几年摊销网站开发工作量
  • 长春网站制作优势吉网传媒网站制作论坛
  • 商城网站怎么做的洛阳网络公司排名
  • 网站架构包括哪些wordpress 插件 论坛
  • 焦作维科网站建设公司制作网站软件手机
  • wordpress产品网站做新标准大学英语网站
  • 免费设计房屋的网站公司企业文化内容
  • 做机械的外贸网站大数据人工智能培训班
  • 化妆品备案查询网站新手学wordpress
  • 建设网站过程视频no.7极简wordpress博客主题
  • 哪个网站可以免费做音乐相册域名 wordpress
  • 资讯平台网站模板青羊区建设和交通网站
  • 湖南智能网站建设网络营销具有哪些特点
  • 网站开发计算机语言的比较长沙网站seo收费
  • 科技网站开发佛山网站制作系统
  • 手机网站会员识别功能app开发公司的管理体系