logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r823021 - in /logging/log4j/trunk: src/changes/ src/main/java/org/apache/log4j/ src/main/java/org/apache/log4j/config/ src/main/java/org/apache/log4j/helpers/ tests/src/java/org/apache/log4j/varia/
Date Thu, 08 Oct 2009 05:24:10 GMT
Author: carnold
Date: Thu Oct  8 05:24:09 2009
New Revision: 823021

URL: http://svn.apache.org/viewvc?rev=823021&view=rev
Log:
Bug 38061: PropertyConfigurator does not support ErrorHandler

Modified:
    logging/log4j/trunk/src/changes/changes.xml
    logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/OptionConverter.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/varia/ErrorHandlerTestCase.java

Modified: logging/log4j/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/changes/changes.xml?rev=823021&r1=823020&r2=823021&view=diff
==============================================================================
--- logging/log4j/trunk/src/changes/changes.xml (original)
+++ logging/log4j/trunk/src/changes/changes.xml Thu Oct  8 05:24:09 2009
@@ -81,6 +81,7 @@
        <action action="add" issue="47142">Add SMTPAppender.formatBody to simplify extension.</action>
        <action action="fix" issue="46404">Failure when toString() of throwable performs
logging in SocketAppender, SocketHubAppender and JDBCAppender.</action>
        <action action="fix" issue="47465">Reading configuration files from a JAR locks
the JAR file</action>
+       <action action="add" issue="38061">PropertyConfigurator does not support configuring
ErrorHandler.</action>
     </release>
 
   

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java?rev=823021&r1=823020&r2=823021&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java Thu Oct 
8 05:24:09 2009
@@ -32,6 +32,9 @@
 import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.Vector;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
 
 import org.apache.log4j.config.PropertySetter;
 import org.apache.log4j.helpers.FileWatchdog;
@@ -46,6 +49,7 @@
 import org.apache.log4j.spi.RendererSupport;
 import org.apache.log4j.spi.ThrowableRenderer;
 import org.apache.log4j.spi.ThrowableRendererSupport;
+import org.apache.log4j.spi.ErrorHandler;
 
 /**
    Allows the configuration of log4j from an external file.  See
@@ -92,7 +96,8 @@
   /**
      Used internally to keep track of configured appenders.
    */
-  protected Hashtable registry = new Hashtable(11);
+  protected Hashtable registry = new Hashtable(11);  
+  private LoggerRepository repository;
   protected LoggerFactory loggerFactory = new DefaultCategoryFactory();
 
   static final String      CATEGORY_PREFIX = "log4j.category.";
@@ -105,6 +110,10 @@
   static final String      RENDERER_PREFIX = "log4j.renderer.";
   static final String      THRESHOLD_PREFIX = "log4j.threshold";
   private static final String      THROWABLE_RENDERER_PREFIX = "log4j.throwableRenderer";
+  private static final String LOGGER_REF	= "logger-ref";
+  private static final String ROOT_REF		= "root-ref";
+  private static final String APPENDER_REF_TAG 	= "appender-ref";  
+  
 
   /** Key for specifying the {@link org.apache.log4j.spi.LoggerFactory
       LoggerFactory}.  Currently set to "<code>log4j.loggerFactory</code>". 
*/
@@ -180,6 +189,17 @@
     paris. Multiple filters are added to the appender in the lexicographic
     order of IDs.
 
+    The syntax for adding an {@link ErrorHandler} to an appender is:
+    <pre>
+    log4j.appender.appenderName.errorhandler=fully.qualified.name.of.filter.class
+    log4j.appender.appenderName.errorhandler.root-ref={true|false}
+    log4j.appender.appenderName.errorhandler.logger-ref=loggerName
+    log4j.appender.appenderName.errorhandler.appender-ref=appenderName
+    log4j.appender.appenderName.errorhandler.option1=value1
+    ...
+    log4j.appender.appenderName.errorhandler.optionN=valueN
+    </pre>
+
     <h3>Configuring loggers</h3>
 
     <p>The syntax for configuring the root logger is:
@@ -452,6 +472,7 @@
   */
   public
   void doConfigure(Properties properties, LoggerRepository hierarchy) {
+	repository = hierarchy;
     String value = properties.getProperty(LogLog.DEBUG_KEY);
     if(value == null) {
       value = properties.getProperty("log4j.configDebug");
@@ -478,7 +499,7 @@
 						     (Level) Level.ALL));
       LogLog.debug("Hierarchy threshold set to ["+hierarchy.getThreshold()+"].");
     }
-
+    
     configureRootCategory(properties, hierarchy);
     configureLoggerFactory(properties);
     parseCatsAndRenderers(properties, hierarchy);
@@ -753,6 +774,26 @@
 	  LogLog.debug("End of parsing for \"" + appenderName +"\".");
 	}
       }
+      final String errorHandlerPrefix = prefix + ".errorhandler";
+      String errorHandlerClass = OptionConverter.findAndSubst(errorHandlerPrefix, props);
+      if (errorHandlerClass != null) {
+    		ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByKey(props,
+					  errorHandlerPrefix,
+					  ErrorHandler.class,
+					  null);
+    		if (eh != null) {
+    			  appender.setErrorHandler(eh);
+    			  LogLog.debug("Parsing errorhandler options for \"" + appenderName +"\".");
+    			  parseErrorHandler(eh, errorHandlerPrefix, props, repository);
+    			  Properties edited = new Properties(props);
+    			  edited.remove(errorHandlerPrefix + "." + ROOT_REF);
+    			  edited.remove(errorHandlerPrefix + "." + LOGGER_REF);
+    			  edited.remove(errorHandlerPrefix + "." + APPENDER_REF_TAG);
+    		      PropertySetter.setProperties(eh, edited, errorHandlerPrefix + ".");
+    			  LogLog.debug("End of errorhandler parsing for \"" + appenderName +"\".");
+    		}
+    	  
+      }
       //configureOptionHandler((OptionHandler) appender, prefix + ".", props);
       PropertySetter.setProperties(appender, props, prefix + ".");
       LogLog.debug("Parsed \"" + appenderName +"\" options.");
@@ -761,7 +802,33 @@
     registryPut(appender);
     return appender;
   }
-
+  
+  private void parseErrorHandler(
+		  final ErrorHandler eh,
+		  final String errorHandlerPrefix,
+		  final Properties props, 
+		  final LoggerRepository hierarchy) {
+		boolean rootRef = OptionConverter.toBoolean(
+					  OptionConverter.findAndSubst(errorHandlerPrefix + ROOT_REF, props), false);
+		if (rootRef) {
+				  eh.setLogger(hierarchy.getRootLogger());
+	    }
+		String loggerName = OptionConverter.findAndSubst(errorHandlerPrefix + LOGGER_REF , props);
+		if (loggerName != null) {
+			Logger logger = (loggerFactory == null) ? hierarchy.getLogger(loggerName)
+			                : hierarchy.getLogger(loggerName, loggerFactory);
+			eh.setLogger(logger);
+		}
+		String appenderName = OptionConverter.findAndSubst(errorHandlerPrefix + APPENDER_REF_TAG,
props);
+		if (appenderName != null) {
+			Appender backup = parseAppender(props, appenderName);
+			if (backup != null) {
+				eh.setBackupAppender(backup);
+			}
+		}
+  }
+				
+  
   void parseAppenderFilters(Properties props, String appenderName, Appender appender) {
     // extract filters and filter options from props into a hashtable mapping
     // the property name defining the filter class to a list of pre-parsed

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java?rev=823021&r1=823020&r2=823021&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/config/PropertySetter.java Thu Oct
 8 05:24:09 2009
@@ -25,6 +25,7 @@
 import org.apache.log4j.helpers.LogLog;
 import org.apache.log4j.helpers.OptionConverter;
 import org.apache.log4j.spi.OptionHandler;
+import org.apache.log4j.spi.ErrorHandler;
 
 import java.beans.BeanInfo;
 import java.beans.IntrospectionException;
@@ -130,7 +131,7 @@
         
 	String value = OptionConverter.findAndSubst(key, properties);
         key = key.substring(len);
-        if ("layout".equals(key) && obj instanceof Appender) {
+        if (("layout".equals(key) || "errorhandler".equals(key)) && obj instanceof
Appender) {
           continue;
         }
         //
@@ -280,6 +281,9 @@
       }
     } else if (Priority.class.isAssignableFrom(type)) {
       return OptionConverter.toLevel(v, (Level) Level.DEBUG);
+    } else if (ErrorHandler.class.isAssignableFrom(type)) {
+      return OptionConverter.instantiateByClassName(v, 
+	  ErrorHandler.class, null);
     }
     return null;
   }

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/OptionConverter.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/OptionConverter.java?rev=823021&r1=823020&r2=823021&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/OptionConverter.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/OptionConverter.java Thu Oct
 8 05:24:09 2009
@@ -20,7 +20,6 @@
 import java.util.Properties;
 import java.net.URL;
 import java.io.InterruptedIOException;
-import java.lang.reflect.InvocationTargetException;
 
 import org.apache.log4j.Level;
 import org.apache.log4j.spi.Configurator;

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/varia/ErrorHandlerTestCase.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/varia/ErrorHandlerTestCase.java?rev=823021&r1=823020&r2=823021&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/varia/ErrorHandlerTestCase.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/varia/ErrorHandlerTestCase.java Thu
Oct  8 05:24:09 2009
@@ -29,6 +29,7 @@
 import org.apache.log4j.util.LineNumberFilter;
 import org.apache.log4j.util.ControlFilter;
 import org.apache.log4j.xml.DOMConfigurator;
+import org.apache.log4j.PropertyConfigurator;
 
 public class ErrorHandlerTestCase extends TestCase {
 
@@ -78,6 +79,25 @@
 
     assertTrue(Compare.compare(FILTERED, "witness/fallback1"));
   }
+  
+  public void test2() throws Exception {
+    PropertyConfigurator.configure("input/fallback1.properties");
+    Appender primary = root.getAppender("PRIMARY");
+    ErrorHandler eh = primary.getErrorHandler();
+    assertNotNull(eh);
+
+    common();
+
+    ControlFilter cf = new ControlFilter(new String[]{TEST1_PAT,
+					       EXCEPTION1, EXCEPTION2, EXCEPTION3});
+
+    Transformer.transform(TEMP, FILTERED, new Filter[] {cf,
+							new LineNumberFilter(),
+                            new JunitTestRunnerFilter()});
+
+
+    assertTrue(Compare.compare(FILTERED, "witness/fallback1"));
+  }
 
   void common() {
     int i = -1;



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message