【Springboot之搜索日志妙招】在日志上打印请求唯一log标识

在每次请求中打出的每条日志中添加统一的请求唯一标识。通过搜索日志唯一标识,这样就可以非常高效
精准排查问题;例如:

2018-12-21 10:21:26.329 [http-nio-8080-exec-2] [my-server-logtag] [] INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 18 ms 
2018-12-21 10:21:26.363 [http-nio-8080-exec-2] [my-server-logtag] [aed35d43-3fa2-46e1-bb72-ce9f50c3fe4d] ERROR com.code3.modules.controller.MyController - 日志1 
2018-12-21 10:21:26.367 [http-nio-8080-exec-2] [my-server-logtag] [aed35d43-3fa2-46e1-bb72-ce9f50c3fe4d] ERROR com.code3.advice.GlobalExceptionHandler - 系统异常,{} 
com.code3.base.BusinessException: 异常信息1
    at com.code3.modules.controller.MyController.logtag(MyController.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at 

通过日志标识:aed35d43-3fa2-46e1-bb72-ce9f50c3fe4d,就可以精准定位

原创:https://www.jianshu.com/p/b74ade8bbb57

目录

  • 自定义日志标识生成拦截器
  • 使用日志标识生成拦截器
  • 演示

自定义日志标识生成拦截器LogInterceptor

package com.interceptors.logtag.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * 

日志拦截器

* * @author zetting * @date 2018/8/27 11:18 */ @Component public class LogInterceptor extends HandlerInterceptorAdapter { private Logger log = LoggerFactory.getLogger(this.getClass()); /** * 日志跟踪标识 */ private static final String TRACE_ID = "TRACE_ID"; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { String traceId = UUID.randomUUID().toString(); if (StringUtils.isEmpty(MDC.get(TRACE_ID))) { MDC.put(TRACE_ID, traceId); } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { MDC.remove(TRACE_ID); } }

使用日志标识生成拦截器

1.在WebMvc配置加上LogInterceptor

package com.interceptors.logtag.configuration;

import com.interceptors.logtag.interceptor.LogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * MVC配置
 *
 * @author zet
 * @date 2018-12-21 22:30
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private LogInterceptor logInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logInterceptor);
    }
}

2.在logback文件加上[%X{TRACE_ID}]



    

    
    
    

    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [${app-name}-${module-name}] [%X{TRACE_ID}] %highlight(%-5level) %cyan(%logger{50}) - %highlight(%msg) %n
        
    

    
        /data/log-center/${app-name}/${module-name}/${filename}.log
        
            /data/log-center/${app-name}/${module-name}/${filename}.%d{yyyy-MM-dd}.log
            30
        
        
            [%date{ISO8601}] [%level] %logger{80} [%thread] [${app-name}-${module-name}][%X{TRACE_ID}] ${app-name}-${module-name} ==> %msg%n
        
    

    
        
        
    

演示

打出的日志标识.jpg

gitee源码:
https://gitee.com/zetting/my-gather/tree/master/springboot-interceptors-logtag

你可能感兴趣的:(【Springboot之搜索日志妙招】在日志上打印请求唯一log标识)