Spring Boot 千分尺traceId和spanId与log4j2一起丢失

xwbd5t1u  于 2023-11-17  发布在  Spring
关注(0)|答案(2)|浏览(249)

在我将我的应用程序从spring Boot 2.7迁移到3.1.5之后,我无法将traceId和spanId记录到logger。
我将sleuth迁移到micromerter,如迁移指南中所述,并将log4j2.xml文件编辑为推荐的格式[%X{traceId:-}, %X{spanId:-}],但它没有记录,如您所见:

2023-11-01 16:38:15.854+0100 [level=INFO ][,]  INFO MBP-14 --- [ctor-http-nio-3] com.example.demo.Controller : Hello, World!

字符串
vs

2023-11-01 16:38:42.896+0100 [level=INFO ][{spanId=fb964679c404b92f, traceId=fb964679c404b92f}]  INFO MBP-14 --- [ctor-http-nio-2] com.example.demo.Controller : Hello, World!


2.7.7依赖关系:

extra["springCloudVersion"] = "2021.0.8"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.springframework.cloud:spring-cloud-starter-sleuth")
    implementation("org.springframework.boot:spring-boot-starter-log4j2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

configurations {
    all {
        exclude(module = "logback-classic")
        exclude(group = "org.springframework.boot", module = "spring-boot-starter-logging")
        exclude(group = "org.slf4j", module = "slf4j-log4j12")
    }
}


3.1.5依赖关系:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.micrometer:micrometer-tracing-bridge-brave")
    implementation("io.zipkin.reporter2:zipkin-reporter-brave")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.springframework.boot:spring-boot-starter-log4j2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

configurations {
    all {
        exclude(module = "logback-classic")
        exclude(group = "org.springframework.boot", module = "spring-boot-starter-logging")
        exclude(group = "org.slf4j", module = "slf4j-log4j12")
    }
}


我用分支 2.73.1 准备了repo here,可以通过调用 hello 端点来复制它。

nhhxz33t

nhhxz33t1#

您正在覆盖Sping Boot 为您准备的log4j配置,因此无论您在logging.pattern.level属性中做什么都是无用的。
最简单的修复方法是删除log4j2-spring.xml文件并设置以下属性:

logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]

字符串
如果您需要该文件,请查看文档如何包含Sping Boot log4j2配置文件。

avwztpqn

avwztpqn2#

Hooks.enableAutomaticContextPropagation()必须添加到main()函数中,然后它才能工作

相关问题