如何在log4j2中以[traceid,spanid]格式获取trace id和span id?

zvms9eto  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(1011)

如何在log4j2中获取trace id和span id,而不是在[traceid,spanid]中获取?
预期输出:[appname,5a59b2372d9a3814,5a59b2372d9a3814]
实际输出:[loglevel=error]--2021-01-21 11:30:32489+0530--http-nio-8080-exec-1 com.springboot.test.aspect.myaspect--asnid=-message=“logging key:”,traceid=f19556b82d98bf86,executiontimeseconds=23
我的log4j2:尝试添加以下注解属性,但在日志中看不到跟踪id

<Property name="LOG_PATTERN">[logLevel=%-5p] -- %d %d{Z} -- %t %c -- asnId=%X{ASN} - %m%n</Property>
        <property name="log.level">${bundle:DEV:logLevel}</property>
         <!--  <property name="rolling.file.encoder.pattern"
              value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %-5level : loggerName=&quot;%logger{36}&quot; threadName=&quot;%thread&quot;  appName=&quot;${springAppName:-}&quot; trace=&quot;%X{X-B3-TraceId:-}&quot; span=&quot;%X{X-B3-SpanId:-}&quot; spanName=&quot;%X{X-Span-Name:-}&quot; parent=&quot;%X{X-B3-ParentSpanId:-}&quot; exportable=&quot;%X{X-Span-Export:-}&quot; pid=&quot;${PID:-}&quot; txnId=&quot;%X{txnId}&quot; %msg%n"/> -->
         <!--   <property name="PATTERN" value="%h %l %u [%date{dd/MMM/yyyy:HH:mm:ss.SSS}] &quot;%r&quot; %s %b &quot; &quot;%i{User-Agent}&quot; [trace=%responseHeader{X-B3-TraceId},span=%i{X-B3-SpanId}] %D"/> -->
    </Properties>

方面类:
但是在我的方面类的日志中,我可以看到跟踪id

public class MyAspect {

    private final static Logger logger = LogManager.getLogger(MyAspect.class);
    long startTime = new Date().getTime();

    @Autowired
    Tracer tracer;

      @Around("allControllerMethods() && args(..,@annotation(org.springframework...RequestBody) requestBody) ")
      public Object controllerEvents(ProceedingJoinPoint jp, Object requestBody) throws Throwable {

         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
             HttpServletRequest request = attributes.getRequest();
             MethodSignature signature = (MethodSignature) jp.getSignature();
              Method method = signature.getMethod();
              Object resObject = jp.proceed();

              logger.error("traceId", tracer.currentSpan().context().traceIdString());

        }
    }

聚甲醛:

<dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave</artifactId>
            <version>5.12.6</version>
        </dependency> 
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>1.4.2.RELEASE</version>
        </dependency>

配置处理程序:

@Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

请注意,在我没有使用log4j的测试项目中,它是如预期的那样出现的。

yshpjwxd

yshpjwxd1#

我可以通过以下更改获得traceid和spanid:1)添加到log4j

<Property name="LOG_PATTERN">[logLevel=%-5p]-- [%X{traceId}/%X{spanId}] -- %d %d{Z} -- %t %c - %m%n</Property>

2) 添加属性:

request.logging.shouldLog=true

3) 添加

@Value("${request.logging.shouldLog}")
    private boolean shouldLog;

//    @Override
    protected boolean shouldLog(HttpServletRequest request) {
        return shouldLog;
    }

相关问题