Correlation ID missing in logs after enabling Log4j2 with Micrometer setup in Spring Boot 3.2.0

57 Views Asked by At

I recently implemented Micrometer setup in my Spring Boot 3.2.0 application to enhance tracing and metrics collection, which worked fine. However, after enabling Log4j2 for improved logging, I noticed that the correlation ID is no longer appearing in the logs.

Here's the dependency setup in my pom.xml for both Micrometer and 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>

My Code GIT Hub Link https://github.com/prabagaran-sellamuthu/correlation-id/tree/main/src/main/java/com/stacktobasics/correlationidsspringboot3

Before enabling Log4j2, I could see the correlation ID in my logs,n. Here's an example log statement:

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

However, after enabling Log4j2, the correlation ID is no longer present in the logs, making it difficult to trace requests effectively.

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

What could be causing this issue? How can I ensure that the correlation ID continues to appear in the logs even after enabling Log4j2? Any insights or suggestions would be greatly appreciated. Thank you!

1

There are 1 best solutions below

0
Prabagaran Sellamuthu On

For Log4j2.xml add this pattern will print the trace-id an 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" />

for log-back in application.yml

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