[logback-dev] svn commit: r2091 - in logback/trunk: logback-classic/src/main/java/ch/qos/logback/classic/sift logback-classic/src/test/input/joran/hoard logback-classic/src/test/input/joran/sift logback-classic/src/test/java/ch/qos/logback/classic logback-classic/src/test/java/ch/qos/logback/classic/sift logback-core/src/main/java/ch/qos/logback/core/sift

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Dec 18 22:33:27 CET 2008


Author: ceki
Date: Thu Dec 18 22:33:27 2008
New Revision: 2091

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/SiftingAppender.java
      - copied, changed from r2087, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingAppender.java
   logback/trunk/logback-classic/src/test/input/joran/sift/
      - copied from r2086, /logback/trunk/logback-classic/src/test/input/joran/hoard/
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/PackageTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java
Removed:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingAppender.java
   logback/trunk/logback-classic/src/test/input/joran/hoard/
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardAction.java
   logback/trunk/logback-classic/src/test/input/joran/sift/completeCycle.xml
   logback/trunk/logback-classic/src/test/input/joran/sift/hoard0.xml
   logback/trunk/logback-classic/src/test/input/joran/sift/smoke.xml
   logback/trunk/logback-classic/src/test/input/joran/sift/unsetDefaultValueProperty.xml
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/AllClassicTest.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingAppenderBase.java

Log:
SiftingAppender has been largely made generic by virtue of its migration to lb-core

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardAction.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardAction.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardAction.java	Thu Dec 18 22:33:27 2008
@@ -25,8 +25,8 @@
   public void end(InterpretationContext ec, String name) throws ActionException {
     ec.removeInPlayListener(this);
     Object o = ec.peekObject();
-    if (o instanceof HoardingAppender) {
-      HoardingAppender ha = (HoardingAppender) o; 
+    if (o instanceof SiftingAppender) {
+      SiftingAppender ha = (SiftingAppender) o; 
       AppenderFactory appenderFactory = new AppenderFactory(context, seList, ha.getMdcKey());
       ha.setAppenderFactory(appenderFactory);
     }

Copied: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/SiftingAppender.java (from r2087, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingAppender.java)
==============================================================================
--- /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingAppender.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/SiftingAppender.java	Thu Dec 18 22:33:27 2008
@@ -12,6 +12,7 @@
 import org.slf4j.MDC;
 
 import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.sift.AppenderTracker;
 import ch.qos.logback.core.sift.SiftingAppenderBase;
 import ch.qos.logback.core.util.OptionHelper;
 
@@ -25,7 +26,7 @@
  * 
  * @author Ceki Gulcu
  */
-public class HoardingAppender extends SiftingAppenderBase<LoggingEvent, String> {
+public class SiftingAppender extends SiftingAppenderBase<LoggingEvent, String> {
 
 
   String mdcKey;
@@ -57,6 +58,10 @@
       return mdcValue;
     }
   }
+  
+  AppenderTracker<LoggingEvent, String> getAppenderTracker() {
+    return appenderTracker;
+  }
 
 
   @Override

Modified: logback/trunk/logback-classic/src/test/input/joran/sift/completeCycle.xml
==============================================================================
--- /logback/trunk/logback-classic/src/test/input/joran/hoard/completeCycle.xml	(original)
+++ logback/trunk/logback-classic/src/test/input/joran/sift/completeCycle.xml	Thu Dec 18 22:33:27 2008
@@ -3,8 +3,8 @@
 
 <configuration debug="true">
 
-  <appender name="HOARD"
-    class="ch.qos.logback.classic.hoard.HoardingAppender">
+  <appender name="SIFT"
+    class="ch.qos.logback.classic.sift.SiftingAppender">
 
     <mdcKey>cycle</mdcKey>
     <defaultValue>cycleDefault</defaultValue>
@@ -14,7 +14,7 @@
   </appender>
 
   <root level="DEBUG">
-    <appender-ref ref="HOARD" />
+    <appender-ref ref="SIFT" />
   </root>
 
 </configuration>

Modified: logback/trunk/logback-classic/src/test/input/joran/sift/hoard0.xml
==============================================================================
--- /logback/trunk/logback-classic/src/test/input/joran/hoard/hoard0.xml	(original)
+++ logback/trunk/logback-classic/src/test/input/joran/sift/hoard0.xml	Thu Dec 18 22:33:27 2008
@@ -3,7 +3,7 @@
 
 <configuration debug="true">
 
-  <appender name="HOARD"
+  <appender name="SIFT"
     class="ch.qos.logback.classic.hoard.HoardingAppender">
 
     <mdcKey>userid</mdcKey>
@@ -20,7 +20,7 @@
   </appender>
 
   <root level="DEBUG">
-    <appender-ref ref="HOARD" />
+    <appender-ref ref="SIFT" />
   </root>
 
 </configuration>

Modified: logback/trunk/logback-classic/src/test/input/joran/sift/smoke.xml
==============================================================================
--- /logback/trunk/logback-classic/src/test/input/joran/hoard/smoke.xml	(original)
+++ logback/trunk/logback-classic/src/test/input/joran/sift/smoke.xml	Thu Dec 18 22:33:27 2008
@@ -3,8 +3,8 @@
 
 <configuration debug="true">
 
-  <appender name="HOARD"
-    class="ch.qos.logback.classic.hoard.HoardingAppender">
+  <appender name="SIFT"
+    class="ch.qos.logback.classic.sift.SiftingAppender">
 
     <mdcKey>userid</mdcKey>
     <defaultValue>smoke</defaultValue>
@@ -14,7 +14,7 @@
   </appender>
 
   <root level="DEBUG">
-    <appender-ref ref="HOARD" />
+    <appender-ref ref="SIFT" />
   </root>
 
 </configuration>

Modified: logback/trunk/logback-classic/src/test/input/joran/sift/unsetDefaultValueProperty.xml
==============================================================================
--- /logback/trunk/logback-classic/src/test/input/joran/hoard/unsetDefaultValueProperty.xml	(original)
+++ logback/trunk/logback-classic/src/test/input/joran/sift/unsetDefaultValueProperty.xml	Thu Dec 18 22:33:27 2008
@@ -3,17 +3,17 @@
 
 <configuration debug="true">
 
-  <appender name="HOARD"
-    class="ch.qos.logback.classic.hoard.HoardingAppender">
+  <appender name="SIFT"
+    class="ch.qos.logback.classic.sift.SiftingAppender">
 
     <mdcKey>userid</mdcKey>
-    <hoard>
+    <sift>
       <appender name="list-${userid}" class="ch.qos.logback.core.read.ListAppender"/>
-    </hoard>
+    </sift>
   </appender>
 
   <root level="DEBUG">
-    <appender-ref ref="HOARD" />
+    <appender-ref ref="SIFT" />
   </root>
 
 </configuration>

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/AllClassicTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/AllClassicTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/AllClassicTest.java	Thu Dec 18 22:33:27 2008
@@ -36,7 +36,6 @@
     suite.addTest(new JUnit4TestAdapter(ch.qos.logback.classic.turbo.PackageTest.class));
     suite.addTest(new JUnit4TestAdapter(
         ch.qos.logback.classic.sift.PackageTest.class));
-
     return suite;
   }
 }

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/PackageTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/PackageTest.java	Thu Dec 18 22:33:27 2008
@@ -0,0 +1,19 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.classic.sift;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+ at RunWith(Suite.class)
+ at SuiteClasses({SiftingAppenderTest.class})
+public class PackageTest  {
+}
\ No newline at end of file

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/SiftingAppenderTest.java	Thu Dec 18 22:33:27 2008
@@ -0,0 +1,95 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.classic.sift;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.slf4j.MDC;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.classic.util.TeztConstants;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.read.ListAppender;
+import ch.qos.logback.core.sift.AppenderTracker;
+import ch.qos.logback.core.util.StatusPrinter;
+
+public class SiftingAppenderTest {
+
+  static String PREFIX = TeztConstants.TEST_DIR_PREFIX + "input/joran/sift/";
+  		
+  LoggerContext loggerContext = new LoggerContext();
+  Logger logger = loggerContext.getLogger(this.getClass().getName());
+  Logger root = loggerContext.getLogger(LoggerContext.ROOT_NAME);
+
+  void configure(String file) throws JoranException {
+    JoranConfigurator jc = new JoranConfigurator();
+    jc.setContext(loggerContext);
+    jc.doConfigure(file);
+  }
+
+  @Test
+  public void unsetDefaultValueProperty() throws JoranException {
+    configure(PREFIX + "unsetDefaultValueProperty.xml");
+    logger.debug("hello");
+    SiftingAppender ha = (SiftingAppender) root.getAppender("SIFT");
+    assertFalse(ha.isStarted());
+    
+  }
+
+  @Test
+  public void smoke() throws JoranException {
+    configure(PREFIX + "smoke.xml");
+    logger.debug("smoke");
+    long timestamp = 0;
+    SiftingAppender ha = (SiftingAppender) root.getAppender("SIFT");
+    ListAppender<LoggingEvent> listAppender = (ListAppender<LoggingEvent>) ha.getAppenderTracker().get("smoke", timestamp);
+    
+    StatusPrinter.print(loggerContext);
+    assertNotNull(listAppender);
+    List<LoggingEvent> eventList = listAppender.list;
+    assertEquals(1, listAppender.list.size());
+    assertEquals("smoke", eventList.get(0).getMessage());
+  }
+
+  @Test
+  public void testWholeCycle() throws JoranException {
+    String mdcKey = "cycle";
+    configure(PREFIX + "completeCycle.xml");
+    MDC.put(mdcKey, "a");
+    logger.debug("smoke");
+    long timestamp = System.currentTimeMillis();
+    SiftingAppender ha = (SiftingAppender) root.getAppender("SIFT");
+    ListAppender<LoggingEvent> listAppender = (ListAppender<LoggingEvent>) ha.getAppenderTracker().get("a", timestamp);
+    StatusPrinter.print(loggerContext);
+    
+    assertNotNull(listAppender);
+    List<LoggingEvent> eventList = listAppender.list;
+    assertEquals(1, listAppender.list.size());
+    assertEquals("smoke", eventList.get(0).getMessage());
+
+    MDC.remove(mdcKey);
+    LoggingEvent le = new LoggingEvent("x", logger, Level.INFO, "hello", null, null);
+    le.setTimeStamp(timestamp+AppenderTracker.THRESHOLD*2);
+    ha.doAppend(le);
+    assertFalse(listAppender.isStarted());
+    assertEquals(1, ha.getAppenderTracker().keyList().size());
+    assertEquals("cycleDefault", ha.getAppenderTracker().keyList().get(0));
+    
+  }
+}

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingAppenderBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingAppenderBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingAppenderBase.java	Thu Dec 18 22:33:27 2008
@@ -9,7 +9,6 @@
  */
 package ch.qos.logback.core.sift;
 
-
 import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.UnsynchronizedAppenderBase;
 import ch.qos.logback.core.joran.spi.JoranException;
@@ -25,10 +24,12 @@
  * 
  * @author Ceki Gulcu
  */
-public abstract class SiftingAppenderBase<E, K> extends UnsynchronizedAppenderBase<E> {
+public abstract class SiftingAppenderBase<E, K> extends
+    UnsynchronizedAppenderBase<E> {
 
   protected AppenderTracker<E, K> appenderTracker = new AppenderTrackerImpl<E, K>();
-  //Map<String, Appender<LoggingEvent>> appenderMap = new Hashtable<String, Appender<LoggingEvent>>();
+  // Map<String, Appender<LoggingEvent>> appenderMap = new Hashtable<String,
+  // Appender<LoggingEvent>>();
 
   String mdcKey;
   String defaultValue;
@@ -41,18 +42,7 @@
 
   @Override
   public void start() {
-    int errors = 0;
-    if (OptionHelper.isEmpty(mdcKey)) {
-      errors++;
-      addError("The \"mdcKey\" property must be set");
-    }
-    if (OptionHelper.isEmpty(defaultValue)) {
-      errors++;
-      addError("The \"defaultValue\" property must be set");
-    }
-    if (errors == 0) {
-      super.start();
-    }
+    super.start();
   }
 
   @Override
@@ -63,18 +53,18 @@
   }
 
   abstract protected K getDiscriminatingValue(E event);
+
   abstract protected long getTimestamp(E event);
-   
+
   @Override
   protected void append(E event) {
     if (!isStarted()) {
       return;
     }
 
-    
     K value = getDiscriminatingValue(event);
     long timestamp = getTimestamp(event);
-    
+
     Appender<E> appender = appenderTracker.get(value, timestamp);
 
     if (appender == null) {
@@ -108,5 +98,4 @@
     return defaultValue;
   }
 
-
 }


More information about the logback-dev mailing list