1

I'm not getting mdc value in logback

can you please help me

Java Code:

       String traceId = MDC.get(getJlgConfiguration().getTraceId());
        String sessionId = MDC.get(getJlgConfiguration().getSessionId());

Logback.xml

<configuration scan="true" scanPeriod="60" debug="true">

    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %class{0}.%M %L, ole-session-id:%mdc{ole-session-id:-NONE}, jlg-trace-id:%mdc{jlg-trace-id:-NONE} - %msg%n
        </Pattern>
    </encoder>
    <root level="info">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>
Aswad Shaik
  • 53
  • 1
  • 10
  • You snippet is get() ? You need to put (Key, Val) and then log and then remove. If you wanna format with your own layout, you'll need to know the key. See the sample on http://logback.qos.ch/manual/mdc.html – HoaPhan Jun 04 '20 at 23:35

2 Answers2

0

You have to use %X instead of %mdc

%d{yyyy-MM-dd HH:mm:ss} - %class{0}.%M %L, ole-session-id:%X{ole-session-id:-NONE}, jlg-trace-id:%X{jlg-trace-id:-NONE} - %msg%n

Checkout the documentation for more information http://logback.qos.ch/manual/mdc.html

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0

Maybe this answer can help, not able to put the values in the MDC

Basically if for example, you're using log4j as the impl underneath slf4j, then you would need log4j config (ConversionPattern) like:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%-5p| %t | [ole-session-id:%X{ole-session-id:-NONE}, jlg-trace-id:%X{jlg-trace-id:-NONE}] | %logger{36} | %m %n</pattern>
    </encoder>
</appender>
Its-Saab
  • 51
  • 3