logback打印日志输出线程ID:mvc拦截器模式

一、前言

经常处理业务问题的同仁,一定都经常与日志打交道。当并发量高、多线程编程时,日志往往是一大堆,为了快速精确的定位、处理问题,我们需要区分各个用户的不同会话请求,需要从一坨坨日志中做链路追踪。

思路:在输出日志的时候,将每个线程的ID同时输出,当然前提是保证每个线程的ID是唯一的。sl4j 提供的一个工具类MDC,支持 logback和log4j,作用就是扩展变量值到日志中并输出。

二、MVC拦截器模式输出线程ID

  1. 加入POM引用
    
        org.springframework.boot
        spring-boot-starter-web
    

     

  2. 自定义日志拦截器
    package cn.wuhg.climbing.service.design.patterns.sessionid.mvcfilter;
    
    import lombok.extern.slf4j.Slf4j;
    import org.slf4j.MDC;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.UUID;
    
    /**
     * 日志拦截器
     */
    @Slf4j
    @Component
    public class LogInterceptor implements HandlerInterceptor {
        // 会话ID
        private final static String SESSION_ID = "sessionId";
    
        @Override
        public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            MDC.put(SESSION_ID, uuid);
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {
            MDC.remove(SESSION_ID);
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        }
    }
    

     

  3. 重写MCV过滤器
    package cn.wuhg.climbing.service.design.patterns.sessionid.mvcfilter;
    
    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.WebMvcConfigurerAdapter;
    
    @Configuration
    public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
        @Autowired
        private LogInterceptor logInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(logInterceptor);
            super.addInterceptors(registry);
        }
    }
    

     

  4. logback配置
    
    
        SpringBootDemo
        
        
        
        
        
            ${LOG_PATH}/${APPDIR}/logs.log
            
            
                
                ${LOG_PATH}/${APPDIR}/logs-%d{yyyy-MM-dd}.%i.log
    			
                
                
                    20MB
                
            
            
            true
            
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{sessionId}] %-5level %class.%method Line:%-3L - %msg%n
                utf-8
            
        
        
            
            
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{sessionId}] %-5level %class.%method Line:%-3L - %msg%n
                utf-8
            
            
            
                debug
            
        
        
        
            
            
        
    

     

  5. Controller接口
    ​
    @LogId
    @GetMapping("/log")
        public void logId(){
        log.info("测试slf4j日志线程ID");
    }
    
    ​

     

  6. 日志截图

三、往期推荐

logback打印日志输出线程ID:切面模式

 

作者:Smile潇洒Tel 

转载请注明出处,谢谢合作!

你可能感兴趣的:(logback,slf4j,logback,log4j2,slf4j,java)