[logback-dev] svn commit: r2128 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/net main/java/ch/qos/logback/classic/spi test/java/ch/qos/logback/classic/net

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Jan 15 20:02:33 CET 2009


Author: ceki
Date: Thu Jan 15 20:02:33 2009
New Revision: 2128

Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppender_GreenTest.java

Log:

Fixes LBCLASSIC-104.

The prepareForDeferredProcessing() method in LoggingEvent now  creates a copy mdcPropertiesMap. 
It follows that if the MDC is subsequently modified, the logging events are not impacted.

SMTPAppender in lb-classic now invokes event.prepareForDeferredProcessing() method in subAppend().

Added relevant test case.

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java	Thu Jan 15 20:02:33 2009
@@ -70,7 +70,7 @@
    * a cyclic buffer.
    */
   protected void subAppend(LoggingEvent event) {
-    event.getThreadName();
+    event.prepareForDeferredProcessing();
     cb.add(event);
     // addInfo("Added event to the cyclic buffer: " + event.getMessage());
   }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	Thu Jan 15 20:02:33 2009
@@ -14,6 +14,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.slf4j.MDC;
@@ -177,14 +178,18 @@
 
   /**
    * This method should be called prior to serializing an event. It should also
-   * be called when using asynchronous logging.
+   * be called when using asynchronous or deferred logging.
    * 
    * <p> Note that due to performance concerns, this method does NOT extract
-   * caller data. It is the responsability of the calller to extract caller
+   * caller data. It is the responsibility of the caller to extract caller
    * information.
    */
   public void prepareForDeferredProcessing() {
     this.getThreadName();
+    // fixes http://jira.qos.ch/browse/LBCLASSIC-104
+    if (mdcPropertyMap != null) {
+      mdcPropertyMap = new HashMap<String, String>(mdcPropertyMap);
+    }
   }
 
   public LoggerRemoteView getLoggerRemoteView() {

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppender_GreenTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppender_GreenTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppender_GreenTest.java	Thu Jan 15 20:02:33 2009
@@ -14,6 +14,7 @@
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.slf4j.MDC;
 
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
@@ -27,6 +28,7 @@
 import ch.qos.logback.core.Layout;
 import ch.qos.logback.core.joran.spi.JoranException;
 import ch.qos.logback.core.testUtil.RandomUtil;
+import ch.qos.logback.core.util.StatusPrinter;
 
 import com.icegreen.greenmail.util.GreenMail;
 import com.icegreen.greenmail.util.GreenMailUtil;
@@ -46,6 +48,7 @@
 
   @Before
   public void setUp() throws Exception {
+    MDC.clear();
     ServerSetup serverSetup = new ServerSetup(port, "localhost",
         ServerSetup.PROTOCOL_SMTP);
     greenMail = new GreenMail(serverSetup);
@@ -74,7 +77,7 @@
     PatternLayout layout = new PatternLayout();
     layout.setContext(lc);
     layout.setFileHeader(HEADER);
-    layout.setPattern("%-4relative [%thread] %-5level %class - %msg%n");
+    layout.setPattern("%-4relative %mdc [%thread] %-5level %class - %msg%n");
     layout.setFileFooter(FOOTER);
     layout.start();
     return layout;
@@ -111,6 +114,7 @@
     logger.debug("hello");
     logger.error("en error", new Exception("an exception"));
     
+    StatusPrinter.print(lc);
     MimeMultipart mp =  verify(TEST_SUBJECT);
     String body = GreenMailUtil.getBody(mp.getBodyPart(0));
     assertTrue(body.startsWith(HEADER.trim()));
@@ -118,6 +122,25 @@
   }
 
   @Test
+  public void LBCLASSIC_104() throws Exception {
+    buildSMTPAppender();
+    smtpAppender.setLayout(buildPatternLayout(lc));
+    smtpAppender.start();
+    logger.addAppender(smtpAppender);
+    MDC.put("key", "val");
+    logger.debug("hello");
+    MDC.clear();
+    logger.error("en error", new Exception("an exception"));
+    
+    MimeMultipart mp =  verify(TEST_SUBJECT);
+    String body = GreenMailUtil.getBody(mp.getBodyPart(0));
+    assertTrue(body.startsWith(HEADER.trim()));
+    assertTrue(body.contains("key=val"));
+    assertTrue(body.endsWith(FOOTER.trim()));
+  }
+
+  
+  @Test
   public void html() throws Exception {
     buildSMTPAppender();
     smtpAppender.setLayout(buildHTMLLayout(lc));


More information about the logback-dev mailing list