Python 官方文档:入门教程 => 点击学习
目录1.原理2.实现3.过滤器4.logback.xml5.返回体6.效果日志前言: 在微服务环境中,我们经常使用Skywalking、CAT等去实现整体请求链路的追踪,但是这个整体
前言:
在微服务环境中,我们经常使用Skywalking
、CAT等去实现整体请求链路的追踪,但是这个整体运维成本高,架构复杂,我们来使用MDC
通过Log来实现一个轻量级的会话事务跟踪功能。
MDC org.sl4j.MDC
其实内部就是ThreadLocal,MDC提供了put/get/clear等几个核心接口,用于操作ThreadLocal中的数据;ThreadLocal中的K-V,可以在logback.xml中声明,最终将会打印在日志中。
// java代码
MDC.put("userId","laker");
// logback.xml
%X{userId}
例如:
<property name="pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level [%X{userId}] %logger{20} - %msg%n"/>
整体流程如下:
userId:laker
。requestId
requestId
。userId
和requestId
去日志中捞请求链路。@Order(value = Ordered.HIGHEST_PRECEDENCE + 100)
@Component
@WEBFilter(filterName = "MDCFilter", urlPatterns = "/*")
public class MDCFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(httpservletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
try {
MDC.put("userId", "laker");
MDC.put("requestId", IdUtil.fastUUID());
} catch (Exception e) {
//
}
try {
filterChain.doFilter(httpServletRequest, httpServletResponse);
} finally {
MDC.clear();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_HOME" value="logs"/>
<property name="encoding" value="UTF-8"/>
<appender name="DEFAULT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/test.log</file>
<Append>true</Append>
<prudent>false</prudent>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{50} %line - %m%n</pattern>
</encoder>
<!-- 按天回滚 daily -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--归档日志文件名-->
<FileNamePattern>${LOG_HOME}/test.log.%d{yyyy-MM-dd}</FileNamePattern>
<!-- 最多保存15天历史文件 -->
<maxHistory>15</maxHistory>
</rollingPolicy>
</appender>
<!-- 日志输出格式 -->
<property name="log.pattern"
value="%d{HH:mm:ss.SSS} [%thread] %-5level [%X{userId}|%X{requestId}] %logger{20} - [%method,%line] - %msg%n"/>
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{50} %line - %m%n</pattern>
</encoder>
</appender>
<logger name="com.test.demo" level="DEBUG">
<appender-ref ref="DEFAULT"/>
</logger>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="DEFAULT"/>
<appender-ref ref="console"/>
</root>
</configuration>
public class Response<T> {
@apiModelProperty(notes = "响应码,非200 即为异常", example = "200")
private final int code;
@ApiModelProperty(notes = "响应消息", example = "提交成功")
private final String msg;
@ApiModelProperty(notes = "响应数据")
private final T data;
@ApiModelProperty(notes = "请求id")
private final String requestId;
public Response(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
this.requestId = MDC.get("requestId");
}
响应:
{
code: 200,
msg: "",
requestId: "74a269a8-3cb4-417e-853c-b968b77cce23"
}
日志:
18:37:15.997 [http-NIO-8080-exec-1] INFO [laker|90717490-5ef4-4e46-bc2c-605952fc3803] c.l.m.c.InfoController - [v2Map,17] - null
18:37:38.980 [http-nio-8080-exec-2] INFO [laker|82bde351-f86e-466f-97a0-c857a0c4c1c9] c.l.m.c.InfoController - [v2Map,17] - null
18:37:39.992 [http-nio-8080-exec-3] INFO [laker|74a269a8-3cb4-417e-853c-b968b77cce23] c.l.m
到此这篇关于使用MDC实现日志链路跟踪的文章就介绍到这了,更多相关MDC实现日志链路跟踪内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 使用MDC实现日志链路跟踪
本文链接: https://lsjlt.com/news/162686.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0