如何以 [traceId, spanId ] 格式获取 log4j2 中的 trace id 和 span id?
How to get trace id and span id in the log4j2, in the format [traceId, spanId ]?
如何在 log4j2 中获取跟踪 ID 和跨度 ID,而不是在 [traceId, spanId ] 中?
预期输出:
[APPNAME,5a59b2372d9a3814,5a59b2372d9a3814]
实际输出:
[logLevel=ERROR] -- 2021-01-21 11:30:32,489 +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="%logger{36}" threadName="%thread" appName="${springAppName:-}" trace="%X{X-B3-TraceId:-}" span="%X{X-B3-SpanId:-}" spanName="%X{X-Span-Name:-}" parent="%X{X-B3-ParentSpanId:-}" exportable="%X{X-Span-Export:-}" pid="${PID:-}" txnId="%X{txnId}" %msg%n"/> -->
<!-- <property name="PATTERN" value="%h %l %u [%date{dd/MMM/yyyy:HH:mm:ss.SSS}] "%r" %s %b " "%i{User-Agent}" [trace=%responseHeader{X-B3-TraceId},span=%i{X-B3-SpanId}] %D"/> -->
</Properties>
看点class:
在我的方面的日志中 class 但是我可以看到跟踪 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());
}
}
pom :
<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 的测试项目中,它符合预期。
我可以通过进行以下更改来获取 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;
}
如何在 log4j2 中获取跟踪 ID 和跨度 ID,而不是在 [traceId, spanId ] 中?
预期输出: [APPNAME,5a59b2372d9a3814,5a59b2372d9a3814]
实际输出: [logLevel=ERROR] -- 2021-01-21 11:30:32,489 +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="%logger{36}" threadName="%thread" appName="${springAppName:-}" trace="%X{X-B3-TraceId:-}" span="%X{X-B3-SpanId:-}" spanName="%X{X-Span-Name:-}" parent="%X{X-B3-ParentSpanId:-}" exportable="%X{X-Span-Export:-}" pid="${PID:-}" txnId="%X{txnId}" %msg%n"/> -->
<!-- <property name="PATTERN" value="%h %l %u [%date{dd/MMM/yyyy:HH:mm:ss.SSS}] "%r" %s %b " "%i{User-Agent}" [trace=%responseHeader{X-B3-TraceId},span=%i{X-B3-SpanId}] %D"/> -->
</Properties>
看点class:
在我的方面的日志中 class 但是我可以看到跟踪 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());
}
}
pom :
<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 的测试项目中,它符合预期。
我可以通过进行以下更改来获取 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;
}