spring-bootlog4j2micrometerdistributed-tracingmicrometer-tracing

Micrometer traceId and spanId are missing with log4j2


after I migrated my app from spring boot 2.7 to 3.1.5, I am not able to log traceId and spanId to the logger.

I migrated sleuth to micromerter, as described in migration guide and edited the log4j2.xml file to recommended format [%X{traceId:-}, %X{spanId:-}] but it is not logged as you can see bellow:

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 dependencies:

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:

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")
    }
}

I prepared repo here with branches 2.7 and 3.1 where it can be reproduced by calling the hello endpoint.


Solution

  • Hooks.enableAutomaticContextPropagation() has to be added to the main() function and then it works