Facing this logback issue in my SpringBoot3 application executed using GraalVm. Using JSONencoder for logs and i see the following difference. The logs printed in the native mode has mdc empty always. While running the application in springboot mode, i do see values populated in mdc map. As per logback, if everything is configured rightly, mdc will be populated automatically.
Log in SpringBoot mode:
Invalid Request.
#error: Validation failed for argument [0] in public io.registry.dto.response.HubResponse ....
{"sequenceNumber":0,"timestamp":1707047416084,"nanoseconds":84264000,"level":"ERROR","threadName":"http-nio-50550-exec-2","loggerName":"io.registry.exception.PlatformRegistryExceptionHandler","context":{"name":"default","birthdate":1707047397944,"properties":{}},
**"mdc": {"path":"/hubs","comment":"test attribute","method":"POST","operator":"jasmine","X-CORRELATION-ID":"d0037334-655e-4e96-88a7-aee4bf00ce3d"},**
"message":"Invalid Request. \n#error: {}","arguments": ["Validation failed for argument [0] in public io.registry.dto.response.HubResponse io.registry.controller.HubController.registerHub(io.registry.model.request.HubRequest,java.lang.String,java.lang.String) throws io.registry.exception.PlatformRegistryException: [Field error in object 'hubRequest' on field 'name': rejected value []; codes [Size.hubRequest.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [hubRequest.name,name]; arguments []; default message [name],15,3]; default message [size must be between 3 and 15]] "],"throwable":null}
Logs in Native mode:
Invalid Request.
#error: Validation failed for argument [0] in public io.registry.dto.response.HubResponse io.registry.controller.HubController.registerHub(io.registry.model.request.HubRequest,java.lang.String,java.lang.String) throws io.registry.exception.PlatformRegistryException: [Field error in object 'hubRequest' on field 'name': rejected value []; codes [Size.hubRequest.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [hubRequest.name,name]; arguments []; default message [name],15,3]; default message [size must be between 3 and 15]]
{"sequenceNumber":0,"timestamp":1707047561881,"nanoseconds":881714000,"level":"ERROR","threadName":"http-nio-50550-exec-4","loggerName":"io.registry.exception.PlatformRegistryExceptionHandler","context":{"name":"default","birthdate":1707047555494,"properties":{}},
**"mdc": {},**
"message":"Invalid Request. \n#error: {}","arguments": ["Validation failed for argument [0] in public io.registry.dto.response.HubResponse io.registry.controller.HubController.registerHub(io.registry.model.request.HubRequest,java.lang.String,java.lang.String) throws io.registry.exception.PlatformRegistryException: [Field error in object 'hubRequest' on field 'name': rejected value []; codes [Size.hubRequest.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [hubRequest.name,name]; arguments []; default message [name],15,3]; default message [size must be between 3 and 15]] "],"throwable":null}
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<appender name="jsonEncoder" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.JsonEncoder"/>
</appender>
<root level="INFO">
<appender-ref ref="jsonEncoder" />
</root>
</configuration>
`