Micrometer traceId and spanId are missing with log4j2

735 views Asked by At

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.

2

There are 2 answers

1
kubele On BEST ANSWER

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

4
Jonatan Ivanov On

You are overriding the log4j config that Spring Boot prepares for you so whatever you do in the logging.pattern.level property us useless.

The easiest fix is deleting the log4j2-spring.xml file and setting the following property:

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

If you need that file please check the docs how to include back the Spring Boot log4j2 config files.