qos-ch / logback

The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch
Other
2.97k stars 1.28k forks source link

MDC variable not getting restored in Java 21 Virtual Threads #798

Open ajaypilaniyawynk opened 5 months ago

ajaypilaniyawynk commented 5 months ago

I am using below message pattern : <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%level] [%thread] [%file:%line] [%X{traceId}] %msg%n

Now suppose I have a code snipped like this -

MDC.put("traceId", "1234");
log.info("Started");
testMongoDao.findById("Test");
log.info("Stopped");

Then I get below logs :

[2024-03-28 16:14:06.522] [INFO] [runner-0] [TestClass.java:70] [1234] Started
[2024-03-28 16:14:06.523] [INFO] [runner-0] [TestClass.java:72] [] Stopped

As you can see in second log line trace id is not printed. Is there any way to solve this? PS: I am using Spring Boot 3.2.3 with virtual threads. On normal threads there is no issue.