I have been trying to create a logback.xml logging pattern for my spring-boot project in which I can get some requirements.
I want to print data in logging pattern (such as process-id, request-id, user request data for a particular API request, user response for the same, etc. ) using MDC but I couldn't find any solution about how MDC communicates with logback.xml
and inserts the required value.
I am new in spring-boot and Java development; kindly suggest me some solution or ideas.
You can add values in the MDC using put
MDC.put("process-id", "1");
And then you define a log pattern that uses these values with %X
%X{process-id}
For example you can define the pattern in the application.properties:
logging.pattern.console=%-4r [%thread] %-5level %X{process-id} - msg%n
Or in the logback.xml
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<Pattern>%-4r [%thread] %-5level %X{process-id} - msg%n</Pattern>
</layout>
</appender>