Sleuth+logback 設置traceid 及自定義信息方式

Sleuth+logback 設置traceid及自定義信息

背景:

分佈式系統中,如何快速定位某個用戶的請求日志?

使用Sleuth生成的traceid可以跟蹤某個請求,但是很多時候我們需要知道traceid 與某個用戶的映射關系,方便定位某個用戶的日志

方案:

@Component
@Order(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER + 1)
public class CustomHttpSpanExtractor extends GenericFilterBean {
    private final Tracer tracer;
    CustomHttpSpanExtractor(Tracer tracer) {
        this.tracer = tracer;
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        Span currentSpan = this.tracer.currentSpan();
        if (currentSpan == null) {
            chain.doFilter(request, response);
            return;
        }
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        try {
          //  mdc(httpRequest,currentSpan.context().traceIdString());
            MDC.put(MdcConstant.USER_ID,"userid-12345");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            chain.doFilter(request, response);
        } finally {
            MDC.clear();//must be,threadLocal
        }
    }}

logback 配置

<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}[${applicationName},%X{X-B3-TraceId:-},%X{userId:-}] [%thread] %-5level %logger{50} - %msg%n</pattern>

logback簡單使用,自定義key

1.logback的pom依賴

<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>4.11</version>
</dependency>

2.配置文件: logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="5000" debug="false">
 <springProperty scope="context" name="springAppName" source="spring.application.name" defaultValue="defaultAppName"/>
 <timestamp key="bySecond" datePattern="yyyy-MM-dd HH:mm:ss"/>   
 <appender name="STASH-REDIS" class="com.jlb.yts.common.util.aop.RedisAppender">
        <key>${springAppName:-}</key>
  <!-- <filter class="ch.qos.logback.classic.filter.LevelFilter">  
            <level>ALL</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter> -->
  <layout>
   <pattern>
    {
    "time": "%d{yyyy-MM-dd HH:mm:ss}",
    "application":"tech_cloud",
    "subapplication":"${springAppName:-}",
    "level": "%level",
    "trace": "%X{X-B3-TraceId:-}",
    "span": "%X{X-B3-SpanId:-}",
    "parent": "%X{X-B3-ParentSpanId:-}",
    "classname": "%logger{100}",
    "msg": "%message"
    }
            </pattern>
  </layout>
    </appender>
    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender"> 
        <!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 -->
        <queueSize>5120</queueSize>
        <appender-ref ref="STASH-REDIS" />
    </appender>
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <encoder charset="UTF-8">
       <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{40} - %msg%n</pattern>
     </encoder>
    </appender>
    <logger name="com.jlb.yts" additivity="true" level="ERROR">
     <appender-ref ref="ASYNC" />
    </logger>
    <logger name="com.jlb.yts" additivity="true" level="INFO">
     <appender-ref ref="ASYNC" />
    </logger>
    <!-- mongodb打印語句 -->
 <logger name="org.springframework.data.mongodb.core" level="DEBUG"/>
    <logger name="com.jlb.yts" additivity="true" level="DEBUG">
     <appender-ref ref="ASYNC" />
    </logger>
    <root level="INFO">
     <appender-ref ref="STDOUT" />
    </root>
</configuration>

3.自定義appender

package com.jlb.yts.common.util.aop;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.spi.StackTraceElementProxy;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import ch.qos.logback.core.filter.Filter;
import com.jlb.yts.common.util.redis.RedisHandle;
import com.jlb.yts.common.util.redis.ReidsConnect;
import net.logstash.logback.layout.LoggingEventCompositeJsonLayout;
import java.lang.reflect.Field;
public class RedisAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
 public static String service;
 
 Layout<ILoggingEvent> layout;
 public void setFilter(Filter<ILoggingEvent> filter) {
  if(filter != null){
   this.addFilter(filter);
  }
 }
 
 String key = null;
 public RedisAppender() {
  layout = new LoggingEventCompositeJsonLayout();
 }
 private final static RedisHandle redisHandle = ReidsConnect.init("redis-log.properties","redis-log.properties");
 @Override
 protected void append(ILoggingEvent event) {
  try {
   LoggingEvent loggingEvent = (LoggingEvent) event;
   String json = layout.doLayout(loggingEvent);
   redisHandle.addList(key,json);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   
  }
 }
 
 public String getKey() {
  return key;
 }
 public void setKey(String key) {
  String prefix = (System.getenv("APPLICATION_ENV") == null)?"dev":System.getenv("APPLICATION_ENV");
  key = prefix + "-" + key;
  this.key = key;
 }
 public Layout<ILoggingEvent> getLayout() {
  return layout;
 }
 public void setLayout(Layout<ILoggingEvent> layout) {
  this.layout = layout;
 }
}

4.代碼實例

//註意導入的包都是slf4j的包
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogbackTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class);
    public static void main(String[] args) {
        LOGGER.trace("logback的--trace日志--輸出瞭");
        LOGGER.debug("logback的--debug日志--輸出瞭");
        LOGGER.info("logback的--info日志--輸出瞭");
        LOGGER.warn("logback的--warn日志--輸出瞭");
        LOGGER.error("logback的--error日志--輸出瞭");
    }
}

5.日志添加自定義KEY

import org.slf4j.MDC;
MDC.put(key, value);

logback.xml 配置

"key":"%X{key:-}"

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: