在 Spring Boot 3.2.0 中使用 Micrometer 设置启用 Log4j2 后,日志中缺少相关 ID

问题描述 投票:0回答:1

我最近在 Spring Boot 3.2.0 应用程序中实现了 Micrometer 设置,以增强跟踪和指标收集,效果很好。但是,在启用 Log4j2 来改进日志记录后,我注意到相关 ID 不再出现在日志中。

这是我的 pom.xml 中 Micrometer 和 Log4j2 的依赖项设置:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing-bridge-brave</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

我的代码 GIT Hub 链接 https://github.com/prabagaran-sellamuthu/correlation-id/tree/main/src/main/java/com/stacktobasics/correlationidsspringboot3

在启用 Log4j2 之前,我可以在日志中看到相关 ID,n。这是一个日志语句示例:

2024-03-24T14:22:18.167Z  INFO 4447 --- [correlation-ids-spring-boot-3] [nio-8080-exec-3] [6600371a03f15877208c8852960f1151-208c8852960f1151] c.s.c.service.HelloWorldService       : Returning hello from service
2024-03-24T14:22:18.752Z  INFO 4447 --- [correlation-ids-spring-boot-3] [nio-8080-exec-4] [6600371a01492df2373384c4c45a4b3e-373384c4c45a4b3e] c.s.c.service.HelloWorldService       : Returning hello from service

但是,启用Log4j2后,日志中不再存在相关ID,导致难以有效跟踪请求。

14:24:18.150 [http-nio-8080-exec-1] INFO  com.stacktobasics.correlationidsspringboot3.api.HelloWorldController - Someone called the /hello endpoint
14:24:18.150 [http-nio-8080-exec-1] INFO  com.stacktobasics.correlationidsspringboot3.service.HelloWorldService - Returning hello from service
14:24:26.750 [http-nio-8080-exec-3] INFO  com.stacktobasics.correlationidsspringboot3.api.HelloWorldController - Someone called the /hello endpoint
14:24:26.750 [http-nio-8080-exec-3] INFO  com.stacktobasics.correlationidsspringboot3.service.HelloWorldService - Returning hello from service

什么可能导致此问题?即使在启用 Log4j2 后,如何确保相关 ID 继续出现在日志中?任何见解或建议将不胜感激。谢谢!

java spring spring-boot log4j2 spring-micrometer
1个回答
0
投票

对于 Log4j2.xml 添加此模式将打印跟踪 id 和 span-id

<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}| %X{traceId} %X{spanId}|%t|%m%n" /><PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}| %X{traceId} %X{spanId}|%t|%m%n" />

application.yml 中的log-back

logging: pattern: level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
    
© www.soinside.com 2019 - 2024. All rights reserved.