package com.xf.config;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j; @Slf4j
public class RequestInterceptor implements HandlerInterceptor {
public static final String REQUEST_ID_KEY = "request-id"; @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
MDC.put(REQUEST_ID_KEY, getRequestId(request));
log.info("拦截一下哦");
return HandlerInterceptor.super.preHandle(request, response, handler);
} @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
// 把requestId添加到响应头,以便其它应用使用
response.addHeader(REQUEST_ID_KEY, MDC.get(REQUEST_ID_KEY));
// 请求完成,从MDC中移除requestId
MDC.remove(REQUEST_ID_KEY);
} public static String getRequestId(HttpServletRequest request) {
String requestId;
String parameterRequestId = request.getParameter(REQUEST_ID_KEY);
String headerRequestId = request.getHeader(REQUEST_ID_KEY);
// 根据请求参数或请求头判断是否有“request-id”,有则使用,无则创建
if (parameterRequestId == null && headerRequestId == null) {
requestId = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + IdUtil.simpleUUID().substring(0, 5);
} else {
requestId = parameterRequestId != null ? parameterRequestId : headerRequestId;
}
return requestId;
}
}
    <property name="log.pattern"
value="%X{request-id} %-5level %logger{200} - [%method,%line] - %msg%n" />

日志添加request-id的更多相关文章

  1. [译]Ocelot - Request Id / Correlation Id

    原文 Ocelot可以通过header的形式发送一个requestid.ocelot会将这个requestid转发到下游服务. 如果在日志配置中设置了IncludeScopes为true,那么requ ...

  2. asp.net中的日志添加和未处理异常的记录

    1.第一次写博客如有错误欢迎纠正.邮箱:Jiangwenyuan0217@163.com: 2.此博客可能对初学者有些帮助,对哪些骨灰级的程序员来说都是分分钟的事了,所以就不用在这里费时间了. 环境说 ...

  3. 给日志添加“复制”效果

    给日志添加如上效果的实现方法: 在日志编辑页面,源代码中,添加如下代码,包裹住 目标内容style1: <div class="cnblogs_code"><di ...

  4. wireshark添加ip.id字段

    wireshark添加ip.id字段 为了在多个设备上追踪同一个数据包. 如果是同一个会话,则可以计算延迟, 如sta和应用服务器慢,这种问题,可以根据这个加上ip.id追踪数据到哪里慢了.    

  5. struts2视频学习笔记 15-17 (访问或添加request属性,文件上传)

    课时15 访问或添加request/session/application属性 1.简单说 page指当前页面.在一个jsp页面里有效 2.request 指从http请求到服务器处理结束,返回响应的 ...

  6. struct2访问或添加request/session/application

    访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute()  {     ActionConte ...

  7. Request.QueryString("id")与Request("id")区别

    Request从几个集合取数据是有顺序的,从前到后的顺序依次是 QueryString,Form,最后是ServerVariables.Request对象按照这样的顺序依次搜索这几个集合中的变量,如果 ...

  8. Exception occurred during processing request: id to load is required for loading

    ERROR Dispatcher:38 - Exception occurred during processing request: id to load is required for loadi ...

  9. 根据请求号(request ID)查找正在运行的sql

    --下面的SQL可以根据Request ID找到对应的Session信息: select * from v$session where paddr in (select addr from v$pro ...

  10. 根据Request ID找到对应的Session信息

    2018年3月15日 13:04 /* Formatted on 2018/3/15 13:04:45 (QP5 v5.256.13226.35538) */ --根据Request ID找到对应的S ...

随机推荐

  1. Linux系统部署Jenkins

    搭建Jenkins,准备搞一个定时任务来自动部署服务.做个记录. 问题写在前头:①建议使用最新版的Jenkins版本,jdk版本要跟Jenkins版本对应(有要求):②最好使用war包部署Jenkin ...

  2. 使用canvas 根据角度画圆弧

    最近收到一个需求,根据角度在平面上画出对应的区域,实际就是 以固定的原点,根据起始角度和结束角度和半径,画出他的区域. 写了一小段,试试 export class Draw {   construct ...

  3. Springboot实现验证码登录

    Springboot实现验证码登录 1.背景 本人近期正在完成毕业设计(旅游信息管理系统)的制作,采用的SpringBoot+Thymeleaf的模式.在登录网站时想要添加验证码验证,通过网上查找资料 ...

  4. 如何开发Vite3插件构建Electron开发环境

    新用户购买<Electron + Vue 3 桌面应用开发>,加小册专属微信群,参与群抽奖,送<深入浅出Electron>.<Electron实战>作者签名版. 1 ...

  5. NC 使用Nginx实现https的反向代理

    summary: [通过Nginx实现NCC的https访问,并解决UClient应用的问题] 1 概述 通过Nginx 安装配置反向代理,实现NC.NCC的https访问. 本文以NCC2005为例 ...

  6. AGC007C Pushing Balls —— 期望的神题

    Problem Link 题意: 序列上按顺序交错有 \(n\) 个球和 \(n+1\) 个洞,即 \(hole_1,ball_1,hole_2,ball_2,\dots,ball_n,hole_{n ...

  7. SQLSever数据库基本操作

    一.SQLSever数据库基本操作 1.创建数据库 use master if exists(select * from sysdatabases where name='SMDB') drop da ...

  8. hyperworks2021位安装教程

    hyperworks2021位安装教程:1.先使用"百度网盘客户端"下载hw21_EN_x64软件安装包到电脑磁盘英文路径文件夹下,并鼠标右击进行解压缩,然后在文件夹内找到hwDe ...

  9. Go实现常用软件设计模式二:工厂模式

    目录: 举个栗子 概念介绍 使用场景 1.举个栗子 类图 ``` @startuml'https://plantuml.com/class-diagramclass Elephant { String ...

  10. 第2-3-8章 分片上传和分片合并的接口开发-文件存储服务系统-nginx/fastDFS/minio/阿里云oss/七牛云oss

    目录 5.10 接口开发-分片上传 5.10.1 分片上传介绍 5.10.2 前端分片上传插件webuploader 5.10.3 后端代码实现 5.10.3.1 接口文档 5.10.3.2 代码开发 ...