Duseja, Sushil | 16 Jul 15:26

Logging NDC value

Hello,

I used event.getNDC() to log the NDC value (where event is a
org.apache.log4j.spi.LoggingEvent object), but it's fetching - 'null'.
Similarly, I used event.getThrowableStrRep() to log the exception value
but that is again fetching a null.

Can anyone let me know the correct way of doing the same? Moreover,
please let me know - how to log the MDC value?

Note - I did set the parameter - locationInfo in the log4j.xml.

Thanks.

 
Curt Arnold | 16 Jul 17:57

Re: Logging NDC value


On Jul 16, 2008, at 8:29 AM, Duseja, Sushil wrote:

> Hello,
>
> I used event.getNDC() to log the NDC value (where event is a
> org.apache.log4j.spi.LoggingEvent object), but it's fetching - 'null'.
> Similarly, I used event.getThrowableStrRep() to log the exception  
> value
> but that is again fetching a null.

event.getNDC() would be expected to be null if no NDC value had been  
set for the thread.  event.getThrowableStrRep() would be expected to  
be null if you did not call one of the logging methods that took an  
exception as the second argument.  Since we don't have access to your  
test app and you haven't described it, there is nothing to indicate  
that anything is wrong.

If you want to set the NDC value, do something like:

NDC.push("Context 1");
logger.debug("Hello, World");
NDC.pop();

If you want to a logging request to include an exception:

try {
...
} catch(Exception e) {
	logger.warn("Unable to complete action", e);
(Continue reading)


Gmane