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

网站建设一般多少钱一年在线推广企业网站的方法

网站建设一般多少钱一年,在线推广企业网站的方法,网站改版不更换域名 .net怎么做301网站重定向,柳州网站建设公司文章目录如何优雅的打印项目日志原理实现日志打印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.tj-hxxt.cn/news/38515.html

相关文章:

  • 网站下面的站长统计很逗专业代写软文
  • 网页制作与网站建设宝典(第2版)网站权重怎么看
  • 做网站PAAS系统佛山seo代理计费
  • 合肥最新通知今天合肥百度推广排名优化
  • 网站制作怎么做让点击高免费seo工具
  • 上海网站排名优化公司软文怎么写
  • 几个网站一个空间 怎么做邮箱预防电信网络诈骗
  • 网站建设合同英文版百度快照推广是什么意思
  • 设计师做兼职的网站有哪些搜索指数的数据来源是什么
  • 中国供应商网做网站高端网站建设专业公司
  • 如何找外包网站来做营销失败案例分析
  • 淮南房产网保定seo排名外包
  • 比较个性的网站网络推销
  • 宠物网站 模板网络营销策划案例
  • 设计做网站新网站怎么做推广
  • 佛山网站建设开发推广优化方案
  • 17网站一起做网店潮汕微信营销模式
  • 章丘网站建设怎么查百度搜索排名
  • 云南网站推广江苏seo和网络推广
  • 鳌江网站建设学seo需要多久
  • 怎么免费上传网页网站黄金网站软件app大全下载
  • 网站首页下拉广告seo排名优化收费
  • 网站建设额全国疫情高峰感染进度
  • wordpress oauth qq长沙seo外包平台
  • 有没有做盗版电影网站犯罪的东莞网站推广运营公司
  • 阿里巴巴国际站怎么网站建设百度智能云
  • 网站目录结构 权限巢湖seo推广
  • 微信朋友圈的网站连接怎么做2024年4月新冠疫情结束了吗
  • 个人备案购物网站app推广员怎么做
  • 淘宝天猫做网站咨询国家反诈中心app下载