javaspring-bootlogginglog4j2elastic-common-schema

Add trace.id and transaction.id Springboot


I have a Springboot micro-service. For logging I'm using Elastic common scheme, implemented using ecs-logging-java.

I want to set the trace.ID and a transaction.ID but I'm not sure how?

Bonus question, I'm I right in thinking trace.ID should be the ID to following the request through multiple system. transaction.ID is just for within the service?


Solution

  • Configure your logging patter as below

    <pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} %thread [%X{trace-id}] [%-5level] %class{0} - %msg%n </pattern>

    Put trace Id in MDC. (MDC belongs to particular thread context)

      `MDC.put("trace-id", "traceid1");`
    

    So whenever your log will print a message, it will print trace id. Follow below artical.http://logback.qos.ch/manual/mdc.html