Search code examples
spring-bootlogginglogbackspring-cloud-sleuth

How to add searchable items to logback, similar to traceId in slueth


I have a springboot application which is using sleuth. Which creates logging you can search on. So the following logging code

log.info("Request from client "+clientId);

will return

@timestamp:Dec 30, 2021 @ 16:52:22.855, message: Request from client client111, traceId: 111 spanId: 111

But as well as having timeStamp, message, traceId and spanId, I also want to add clientId. so that I can have

@timestamp:Dec 30, 2021 @ 16:52:22.855, message: Request from client client111, clientId: client111, traceId: 111 spanId: 111

Now I know I add clientId to logback. But how do I code it in the jave code to pick up the clientId.

Log.info writes to the message only, is there a way I can also get it to write clientId to clentId


Solution

  • Use Mapped Diagnostic Context MDC

    so in my code I have

    MDC.put("clientId",clientId);
    logger.info("received from client"+clientId);
    

    in my logback.xml

     %X{clientId} - %msg%n"