You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by E Thors <et...@yahoo.com> on 2002/07/03 18:28:01 UTC

bug with MDC with patch

I believe I have done my due diligence on this one.
There seems to be a problem with MDC, when you specify
the pattern %X without a clientNumber. The problem is
that the current code does not handle it gracefully.
It throws an exception and blows up the application.
Could not find anything in bug tracking. Tried to
check cvs, but currently seems to be down. I realize
that you should specify the clientNumber. An error in
the log4j properites file should not kill the
application. Below I have included a small program and
log4j.properties file.

I am wondering if someone could explain the pros and
cons of NDC and MDC. I have gone through the archives
of log4j-user. It seems like the general feeling was
that MDC is better. I want to be able to track a
user's session through the web site. So when I hit the
jsp page, I would call MDC.put or NDC.push the session
id. Anything called from the jsp page could be trace
by the session id. My initial unscientific experience
with NDC was that at times the stack seemed to retain
items.

On a related note concerning the usage of the pattern
%X{clientNumber}for MDC seems like it would be nice if
 %X without a clientNumber would print out the entire
Hashtable. This would allow the person debugging an
application that did not have access to the
documentation or source code an easy way to find out
what all of the current context were at that point.
They then could possibly refine the pattern they would
use.

I have gone on too long now. Hopefully someone will
read it.

Thanks,

eric


OS: Win2000
Java:1.3.1
Log4j: 1.2.4

---- TestError.java----------
public class TestError {
   static Logger log = Logger.getLogger("foo");

   public static void main (String[] args) {
      PropertyConfigurator.configure(args[0]);

      System.out.println("start");
      MDC.put("test", "foo");
      log.warn("Warn me");
      log.error("Warn me");
      System.out.println("done");

   }
}
-------------------
------ log4j.properties --------------
log4j.rootCategory=warn, A2, RR, dest2

# A2 is set to be a LF5Appender
log4j.appender.A2=org.apache.log4j.lf5.LF5Appender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[slf5s.start]%d{DATE}[slf5s.DATE]%n\
 
%p[slf5s.PRIORITY]%n%x[slf5s.NDC]%n%t[slf5s.THREAD]%n\
 
%c[slf5s.CATEGORY]%n%l[slf5s.LOCATION]%n%m[slf5s.MESSAGE]%n%n

log4j.appender.dest2.ImmediateFlush=true
log4j.appender.dest2.layout=org.apache.log4j.PatternLayout
log4j.appender.dest2.layout.ConversionPattern=%-4r
[%t] %-5p %c (%X) - %m ~%l%n

! WRITE LOG TO A FILE, ROLL THE FILE AFTER SOME SIZE
log4j.appender.dest2=org.apache.log4j.DailyRollingFileAppender
! This appender will only log messages with priority
equal to or higher than
! the one specified here
log4j.appender.dest2.Threshold=WARN
! Specify the file name (${property_key} gets
substituted with its value)
log4j.appender.dest2.File=log4j.log

# RR is the RollingFileAppender that outputs to a
rolling log
# file called sample.log.

log4j.appender.RR=org.apache.log4j.RollingFileAppender
log4j.appender.RR.File=sample.log

log4j.appender.RR.layout=org.apache.log4j.PatternLayout
log4j.appender.RR.layout.ConversionPattern=[slf5s.start]%d{DATE}[slf5s.DATE]%n\
 
%p[slf5s.PRIORITY]%n%x[slf5s.NDC]%n%t[slf5s.THREAD]%n\
 
%c[slf5s.CATEGORY]%n%l[slf5s.LOCATION]%n%m[slf5s.MESSAGE]%n%n

# Set the max size of the file
log4j.appender.RR.MaxFileSize=500KB
------------------------

-------Error ----------------

java TestError log4j.properties
start
java.lang.NullPointerException
        at java.util.Hashtable.get(Hashtable.java:320)
        at org.apache.log4j.MDC.get0(MDC.java:121)
        at org.apache.log4j.MDC.get(MDC.java:75)
        at
org.apache.log4j.spi.LoggingEvent.getMDC(LoggingEvent.java:228)
        at
org.apache.log4j.helpers.PatternParser$MDCPatternConverter.convert(PatternParser.java:455)
        at
org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:56)
        at
org.apache.log4j.PatternLayout.format(PatternLayout.java:495)
        at
org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:292)
        at
org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:225)
        at
org.apache.log4j.WriterAppender.append(WriterAppender.java:150)
        at
org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221)
        at
org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57)
        at
org.apache.log4j.Category.callAppenders(Category.java:190)
        at
org.apache.log4j.Category.forcedLog(Category.java:375)
        at
org.apache.log4j.Category.warn(Category.java:1024)
        at TestError.main(TestError.java:11)

If it were to exit gracefully, you should see "done".

----- Patch -------------
--- MDC.java    2002-07-03 12:09:01.000000000 -0400
+++ c:/MDC.java 2002-07-03 08:40:54.000000000 -0400
@@ -117,7 +117,7 @@
       return null;
     } else {
       Hashtable ht = (Hashtable)
((ThreadLocalMap)tlm).get();
-      if(ht != null) {
+      if(ht != null && key != null) {
        return ht.get(key);
       } else {
        return null;



__________________________________________________
Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>