Posted: 16 Nov 2016 11:01 EST Last activity: 21 Feb 2017 2:57 EST
How to read MDC fields using custom appender
We are trying to implement a custom appender to send our log messages to a log-aggregation tool (ie, Splunk). In order to do this, we had to extend the pega log4j classes and load our custom logging classes into pega.
This part works perfectly, and we are able to send our log messages and associated stack traces to our log aggregator. Our hangup right now is being able to read the values in the MDC hash map that store useful info thats not part of the raw log message (pegathread,userid, requestorid, etc..)
We are trying to read these values by calling the MDC.get() method, however it always returns null values. Is there a different method/function we should be calling to read these MDC values?
***Updated by Moderator: Marissa to add SR Details***
We have imported com.pega.apache.log4j and com.pega.pegarules.priv.LogContext into our custom appender.
We have tried retrieving the MDC fields with both MDC.get and LogContext.get but both didn’t seem to work. What I have noticed is that pega picks up the new value if we change the value for the mdc field but we can’t see the new value if it’s changed in pega.
So for example if I do LogContext.put("pegathread", "TestingAppender"), I can see the value being changed in the logs to TestingAppender but I can’t get the new value when the value of pegathread is changed to STANDARD.
As it’s a AsyncAppender, the Appender append() is called on a separate dispatcher thread. Hence, MDC.get() returns null. What AsyncAppender does is stores a copy of the MDC in the LoggingEvent when AsyncAppender.doAppend() is called on the original thread itself
So, you can fetch value of any MDC key via event.getMDC(key)