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

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Dec 18 22:11:36 CET 2008


Author: ceki
Date: Thu Dec 18 22:11:36 2008
New Revision: 2087

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderFactoryBase.java
      - copied, changed from r2086, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderFactory.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingJoranConfiguratorBase.java
      - copied, changed from r2086, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingJoranConfigurator.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/sift/
Removed:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/AppenderTracker.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/AppenderTrackerImpl.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/hoard/
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/sift/
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderFactory.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingJoranConfigurator.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/AppenderFactory.java
   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/HoardingAppender.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingJoranConfigurator.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTracker.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTrackerImpl.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingAppenderBase.java

Log:
- heavy refactorization of SiftingAppender

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/AppenderFactory.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/AppenderFactory.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/AppenderFactory.java	Thu Dec 18 22:11:36 2008
@@ -1,49 +1,33 @@
+/**
+ * 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 java.util.ArrayList;
 import java.util.List;
 
 import ch.qos.logback.classic.spi.LoggingEvent;
-import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.Context;
 import ch.qos.logback.core.joran.event.SaxEvent;
-import ch.qos.logback.core.joran.spi.JoranException;
-import ch.qos.logback.core.util.StatusPrinter;
+import ch.qos.logback.core.sift.AppenderFactoryBase;
+import ch.qos.logback.core.sift.SiftingJoranConfiguratorBase;
 
-public class AppenderFactory {
+public class AppenderFactory extends AppenderFactoryBase<LoggingEvent, String>{
 
-  final List<SaxEvent> eventList;
-  Context context;
+  String mdcKey;
   
-  AppenderFactory(Context context, List<SaxEvent> eventList) {
-    this.context = context;
-    this.eventList = new ArrayList<SaxEvent>(eventList);
-    removeHoardElement();
-
-  }
-
-  void removeHoardElement() {
-    eventList.remove(0);
-    eventList.remove(eventList.size() - 1);
-    System.out.println(eventList);
-  }
-
-  Appender<LoggingEvent> buildAppender(Context context, String mdcKey,
-      String mdcValue) throws JoranException {
-    //HoardingContext hoardingContext = new HoardingContext(context, mdcKey,
-    //    mdcValue);
-    HoardingJoranConfigurator hjc = new HoardingJoranConfigurator(mdcKey, mdcValue);
-    hjc.setContext(context);
-
-    hjc.doConfigure(eventList);
-
-    StatusPrinter.print(context);
-
-    return hjc.getAppender();
+  AppenderFactory(Context context, List<SaxEvent> eventList, String mdcKey) {
+      super(context, eventList);
+      this.mdcKey = mdcKey;
   }
 
-  public List<SaxEvent> getEventList() {
-    return eventList;
+  public SiftingJoranConfiguratorBase<LoggingEvent> getSiftingJoranConfigurator(String k) {
+    return new HoardingJoranConfigurator(mdcKey, k);
   }
 
 }

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:11:36 2008
@@ -27,7 +27,7 @@
     Object o = ec.peekObject();
     if (o instanceof HoardingAppender) {
       HoardingAppender ha = (HoardingAppender) o; 
-      AppenderFactory appenderFactory = new AppenderFactory(context, seList);
+      AppenderFactory appenderFactory = new AppenderFactory(context, seList, ha.getMdcKey());
       ha.setAppenderFactory(appenderFactory);
     }
   }

Modified: 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/HoardingAppender.java	Thu Dec 18 22:11:36 2008
@@ -12,9 +12,7 @@
 import org.slf4j.MDC;
 
 import ch.qos.logback.classic.spi.LoggingEvent;
-import ch.qos.logback.core.Appender;
-import ch.qos.logback.core.UnsynchronizedAppenderBase;
-import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.sift.SiftingAppenderBase;
 import ch.qos.logback.core.util.OptionHelper;
 
 /**
@@ -27,19 +25,12 @@
  * 
  * @author Ceki Gulcu
  */
-public class HoardingAppender extends UnsynchronizedAppenderBase<LoggingEvent> {
+public class HoardingAppender extends SiftingAppenderBase<LoggingEvent, String> {
 
-  AppenderTracker<LoggingEvent> appenderTracker = new AppenderTrackerImpl<LoggingEvent>();
-  //Map<String, Appender<LoggingEvent>> appenderMap = new Hashtable<String, Appender<LoggingEvent>>();
 
   String mdcKey;
   String defaultValue;
 
-  AppenderFactory appenderFactory;
-
-  void setAppenderFactory(AppenderFactory appenderFactory) {
-    this.appenderFactory = appenderFactory;
-  }
 
   @Override
   public void start() {
@@ -58,43 +49,22 @@
   }
 
   @Override
-  public void stop() {
-    for (Appender<LoggingEvent> appender : appenderTracker.valueList()) {
-      appender.stop();
-    }
-  }
-
-  @Override
-  protected void append(LoggingEvent loggingEvent) {
-    if (!isStarted()) {
-      return;
-    }
-
+  protected String getDiscriminatingValue(LoggingEvent event) {
     String mdcValue = MDC.get(mdcKey);
-
     if (mdcValue == null) {
-      mdcValue = defaultValue;
+      return defaultValue;
+    } else {
+      return mdcValue;
     }
+  }
 
-    long timestamp = loggingEvent.getTimeStamp();
-    
-    Appender<LoggingEvent> appender = appenderTracker.get(mdcValue, timestamp);
-
-    if (appender == null) {
-      try {
-        appender = appenderFactory.buildAppender(context, mdcKey, mdcValue);
-        if (appender != null) {
-          appenderTracker.put(mdcValue, appender, timestamp);
-        }
-      } catch (JoranException e) {
-        addError("Failed to build appender for " + mdcKey + "=" + mdcValue, e);
-        return;
-      }
-    }
-    appenderTracker.stopStaleAppenders(timestamp);
-    appender.doAppend(loggingEvent);
+
+  @Override
+  protected long getTimestamp(LoggingEvent event) {
+    return event.getTimeStamp();
   }
 
+  
   public String getMdcKey() {
     return mdcKey;
   }
@@ -127,4 +97,6 @@
     this.defaultValue = defaultValue;
   }
 
+
+
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingJoranConfigurator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingJoranConfigurator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/sift/HoardingJoranConfigurator.java	Thu Dec 18 22:11:36 2008
@@ -6,16 +6,13 @@
 
 import ch.qos.logback.classic.spi.LoggingEvent;
 import ch.qos.logback.core.Appender;
-import ch.qos.logback.core.joran.GenericConfigurator;
 import ch.qos.logback.core.joran.action.ActionConst;
 import ch.qos.logback.core.joran.action.AppenderAction;
-import ch.qos.logback.core.joran.action.NestedBasicPropertyIA;
-import ch.qos.logback.core.joran.action.NestedComplexPropertyIA;
-import ch.qos.logback.core.joran.spi.Interpreter;
 import ch.qos.logback.core.joran.spi.Pattern;
 import ch.qos.logback.core.joran.spi.RuleStore;
+import ch.qos.logback.core.sift.SiftingJoranConfiguratorBase;
 
-public class HoardingJoranConfigurator  extends GenericConfigurator {
+public class HoardingJoranConfigurator  extends SiftingJoranConfiguratorBase<LoggingEvent> {
 
   String key;
   String value;
@@ -24,23 +21,13 @@
     this.key = key;
     this.value = value;
   }
+  
   @Override
   protected Pattern initialPattern() {
     return new Pattern("configuration");
   }
   
   @Override
-  protected void addImplicitRules(Interpreter interpreter) {
-    NestedComplexPropertyIA nestedComplexIA = new NestedComplexPropertyIA();
-    nestedComplexIA.setContext(context);
-    interpreter.addImplicitAction(nestedComplexIA);
-    
-    NestedBasicPropertyIA nestedSimpleIA = new NestedBasicPropertyIA();
-    nestedSimpleIA.setContext(context);
-    interpreter.addImplicitAction(nestedSimpleIA);
-  }
-
-  @Override
   protected void addInstanceRules(RuleStore rs) {
     rs.addRule(new Pattern("configuration/appender"), new AppenderAction());
   }

Copied: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderFactoryBase.java (from r2086, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderFactory.java)
==============================================================================
--- /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderFactory.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderFactoryBase.java	Thu Dec 18 22:11:36 2008
@@ -1,3 +1,12 @@
+/**
+ * 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.core.sift;
 
 import java.util.ArrayList;
@@ -8,12 +17,12 @@
 import ch.qos.logback.core.joran.event.SaxEvent;
 import ch.qos.logback.core.joran.spi.JoranException;
 
-public abstract class AppenderFactory<E, K> {
+public abstract class AppenderFactoryBase<E, K> {
 
   final List<SaxEvent> eventList;
   Context context;
   
-  AppenderFactory(Context context, List<SaxEvent> eventList) {
+  protected AppenderFactoryBase(Context context, List<SaxEvent> eventList) {
     this.context = context;
     this.eventList = new ArrayList<SaxEvent>(eventList);
     removeHoardElement();
@@ -26,11 +35,10 @@
     System.out.println(eventList);
   }
 
-  
-  abstract SiftingJoranConfigurator<E> getSiftingJoranConfigurator(K k);
+  public abstract SiftingJoranConfiguratorBase<E> getSiftingJoranConfigurator(K k);
   
   Appender<E> buildAppender(Context context, K k) throws JoranException {
-    SiftingJoranConfigurator<E> sjc = getSiftingJoranConfigurator(k);
+    SiftingJoranConfiguratorBase<E> sjc = getSiftingJoranConfigurator(k);
     sjc.setContext(context);
     sjc.doConfigure(eventList);
     return sjc.getAppender();

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTracker.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTracker.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTracker.java	Thu Dec 18 22:11:36 2008
@@ -19,7 +19,7 @@
   static int THRESHOLD = 30 * 60 * MILLIS_IN_ONE_SECOND; // 30 minutes
 
   void put(K key, Appender<E> value, long timestamp);
-  Appender<E> get(String key, long timestamp);
+  Appender<E> get(K key, long timestamp);
   void stopStaleAppenders(long timestamp);
   List<K> keyList();
   List<Appender<E>> valueList();

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTrackerImpl.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTrackerImpl.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/AppenderTrackerImpl.java	Thu Dec 18 22:11:36 2008
@@ -45,7 +45,7 @@
     moveToTail(entry);
   }
 
-  public synchronized Appender<E> get(String key, long timestamp) {
+  public synchronized Appender<E> get(K key, long timestamp) {
     Entry existing = map.get(key);
     if (existing == null) {
       return null;

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:11:36 2008
@@ -33,9 +33,9 @@
   String mdcKey;
   String defaultValue;
 
-  AppenderFactory<E, K> appenderFactory;
+  AppenderFactoryBase<E, K> appenderFactory;
 
-  void setAppenderFactory(AppenderFactory<E, K> appenderFactory) {
+  public void setAppenderFactory(AppenderFactoryBase<E, K> appenderFactory) {
     this.appenderFactory = appenderFactory;
   }
 

Copied: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingJoranConfiguratorBase.java (from r2086, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingJoranConfigurator.java)
==============================================================================
--- /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingJoranConfigurator.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/sift/SiftingJoranConfiguratorBase.java	Thu Dec 18 22:11:36 2008
@@ -2,22 +2,22 @@
 
 import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.joran.GenericConfigurator;
+import ch.qos.logback.core.joran.action.NestedBasicPropertyIA;
+import ch.qos.logback.core.joran.action.NestedComplexPropertyIA;
 import ch.qos.logback.core.joran.spi.Interpreter;
-import ch.qos.logback.core.joran.spi.RuleStore;
 
-public abstract class SiftingJoranConfigurator<E> extends GenericConfigurator {
+public abstract class SiftingJoranConfiguratorBase<E> extends GenericConfigurator {
 
   @Override
   protected void addImplicitRules(Interpreter interpreter) {
-    // TODO Auto-generated method stub
+    NestedComplexPropertyIA nestedComplexIA = new NestedComplexPropertyIA();
+    nestedComplexIA.setContext(context);
+    interpreter.addImplicitAction(nestedComplexIA);
     
+    NestedBasicPropertyIA nestedSimpleIA = new NestedBasicPropertyIA();
+    nestedSimpleIA.setContext(context);
+    interpreter.addImplicitAction(nestedSimpleIA);
   }
-
-  @Override
-  protected void addInstanceRules(RuleStore rs) {
-    // TODO Auto-generated method stub
-    
-  }
-
-  abstract Appender<E> getAppender();
+  
+  abstract public Appender<E> getAppender();
 }


More information about the logback-dev mailing list