Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MDC to implement Log Link tracking

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces "how to use MDC to achieve log link tracking" related knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Preface

Principle

Realize

Filter

Logback.xml

Return body

Effect log

Preface

In the micro-service environment, we often use Skywalking, CAT and so on to achieve the overall request link tracking, but the overall operation and maintenance cost is high and the architecture is complex, so we use MDC to achieve a lightweight session transaction tracking function through Log.

Principle

In fact, inside MDC org.sl4j.MDC, ThreadLocal,MDC provides several core interfaces such as put/get/clear to manipulate the data in ThreadLocal; Kmuri V in ThreadLocal can be declared in logback.xml and will eventually be printed in the log.

/ / java code MDC.put ("userId", "laker"); / / logback.xml% X {userId}

For example:

Realize

The overall process is as follows:

Users log in to the system and we record userId:laker in our log.

When a user initiates a request, multiple http requests may actually be generated in one request. Here, a requestId can be generated at the front end.

In the return body, return requestId.

The R & D operation and maintenance personnel can request the link from the log according to userId and requestId.

Filter @ 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 ();}

Logback.xml

${LOG_HOME} / test.log true false% d {yyyy-MM-dd HH:mm:ss.SSS} [% t]%-5level% logger {50}% line -% m% n ${LOG_HOME} / test.log.%d {yyyy-MM-dd} 15 ${log.pattern}% d {yyyy-MM-dd HH:mm:ss.SSS} [% t]%-5level% logger {50}% line -% m% n Return body public class Response {@ ApiModelProperty (notes = "response code) "exception", example = "200") private final int code @ ApiModelProperty (notes = "response message", example = "submitted successfully") private final String msg; @ ApiModelProperty (notes = "response data") private final T data; @ ApiModelProperty (notes = "request 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");} effect log

Response

{code: 200, msg: "", requestId: "74a269a8-3cb4-417e-853c-b968b77cce23"}

Journal

18 http-nio-8080-exec-1 37 laker 15.997 [http-nio-8080-exec-1] INFO [laker | 90717490-5ef4-4e46-bc2c-605952fc3803] c.l.m.c.InfoController-[v2MapMagne17]-null 18 laker 37 c.l.m.c.InfoController 38.980 [http-nio-8080-exec-2] INFO [laker | 82bde351-f86e-466f-97a0-c857a0c4c1c9] c.l.m.c.InfoController-[v2Map] 17]-null 18 laker 37INFO 39.992 [http-nio-8080-exec-3] INFO [laker | 74a269a8-3cb4-417e-853c-b968b77cce23] c.l.m.c.InfoController-[v2MapMagazine 17]-null "how to use MDC for Log Link tracking" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report