Search code examples
javaspringspring-bootlogback

Can't see Sleuth spanId or traceId in custom logback


logback configuration is down below

    <appender name="ELK-CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <pattern>
                    <pattern>
                    {
                        "timestamp": "@timestamp",
                        "severity": "%level",
                        "service": "${springAppName:-}",
                        "trace": "%X{traceId:-}",
                        "span": "%X{spanId:-}",
                        "pid": "${PID:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "rest": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

But span or trace is always null ,

{
    "timestamp":"@timestamp",
    "severity":"INFO",
    "service":"springAppName_IS_UNDEFINED",
    "trace":"",
    "span":"",
    "pid":"21524",
    "thread":"reactor-http-nio-3",
    "class":"elk-logger",
    "rest":"test log content"
}

Not sure is there is any configuraiton that i'm missing, Thanks for any advice

I tried %X{X-B3-TraceId:-} ,But still doesn't work

"trace": "%X{X-B3-TraceId:-}",
"span": "%X{X-B3-SpanId:-}",

Solution

  • There are some possible reasons why you can't see Sleuth spanId or traceId in your custom logback configuration.

    • One reason is that you need to include the Sleuth pattern in your log pattern, which is %X{traceId:-} %X{spanId:-} %X{parentId:-}.
    • Another reason is that you need to include the base.xml file from Spring Boot, which contains the default configuration for Sleuth logging.
    • A third reason is that you need to set the logging.level.org.springframework.cloud.sleuth property to DEBUG or higher

    I hope this helps you solve your problem. If you want to learn more about Sleuth and Logback, you can check out these resources: • Get Current Trace ID in Spring Cloud Sleuth: A tutorial on how to get the current trace ID and span ID using the Tracer object.