You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Kitching Simon <Si...@orange.ch> on 2001/02/06 14:44:11 UTC

PATCH: Allow custom priority classes in tags and Property Configurator

There are two situations in which custom Priority classes cannot
be specified:

(a) in PropertyConfigurator input files
(b) in <param> tags passed to filters

This patch adds a "toPriority" method to the OptionsConverter
class which allows priority strings of format "classname#level".

Example (see the PriorityMin and PriorityMax attributes):

	<appender name="UDPVENUS"
class="org.apache.log4j.net.DatagramStringAppender">
           <param name="DatagramHost" value="Venus" />	
           <param name="DatagramPort" value="8300" />	
           <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern"
		    value="%p#%d#%c#%m"/>
           </layout>	  
           <filter class="org.apache.log4j.filters.PriorityRangeFilter">
             <param name="PriorityMin" value="ch.orange.log.Priority#DEBUG0"
/>
             <param name="PriorityMax" value="ch.orange.log.Priority#WARN"
/>
           </filter>  
	</appender>

Any code currently calling
  Priority p = Priority.toPriority(value)
need only be changed to call
  Priority p = OptionConverter.toPriority(value, null)
to allow the extended priority ability.

Patch:

Index: OptionConverter.java
===================================================================
RCS file:
/home/cvspublic/jakarta-log4j/org/apache/log4j/helpers/OptionConverter.java,
v
retrieving revision 1.10
diff -u -r1.10 OptionConverter.java
--- OptionConverter.java	2001/01/17 13:02:24	1.10
+++ OptionConverter.java	2001/02/06 13:36:07
@@ -10,6 +10,8 @@
 
 import java.util.Properties;
 import java.net.URL;
+
+import org.apache.log4j.Priority;
 import org.apache.log4j.Category;
 import org.apache.log4j.Hierarchy;
 import org.apache.log4j.spi.Configurator;
@@ -18,6 +20,7 @@
 
 // Contributors:   Avy Sharell (sharell@online.fr)
 //                 Anders Kristensen
+//                 Simon Kitching
 
 /**
    A convenience class to convert property values to specific types.
@@ -181,6 +184,89 @@
       }
     }
     return dEfault;
+  }
+
+  /**
+     Converts a standard or custom priority level to a Priority object.
+     <p>
+     If <code>value</code> is of form "classname#priority", then the
+     specified class' toPriority method is called to process the specified
+     priority string; if no '#' character is present, then the default
+     org.log4j.Priority class is used to process the priority value.
+     <p>
+     If any error occurs while converting the value to a priority, the
+     dflt value (which may be null) is returned.
+     <p>
+     Case of value is unimportant for the priority level, but is
significant
+     for any class name part present.
+   */
+  public
+  static
+  Priority toPriority(String value, Priority dflt) {
+    if(value == null)
+      return dflt;
+
+    int hashIndex = value.indexOf('#');
+    if (hashIndex == -1) {
+      // no class name specified : use standard Priority class
+      return Priority.toPriority(value, dflt);
+    }
+
+    Priority result = dflt;
+
+    String clazz = value.substring(0, hashIndex);
+    String priorityName = value.substring(hashIndex+1);
+
+    LogLog.debug(
+      "toPriority"
+      + ":class=[" + clazz + "]"
+      + ":pri=[" + priorityName + "]");
+
+    try {
+      Class customPriority = Class.forName(clazz);
+
+      // get a ref to the specified class' static method
+      // toPriority(String, org.apache.log4j.Priority)
+      Class[] paramTypes = new Class[] {
+          String.class,
+          org.apache.log4j.Priority.class
+        };
+      java.lang.reflect.Method toPriorityMethod =
+        customPriority.getMethod("toPriority", paramTypes);
+
+      // now call the toPriority method, passing priority string + default
+      Object[] params = new Object[] {priorityName, dflt};
+      Object o = toPriorityMethod.invoke(null, params);
+
+      result = (Priority) o;
+    }
+    catch(ClassNotFoundException e) {
+      LogLog.error("custom priority class [" + clazz + "] could not be
loaded");
+    }
+    catch(NoSuchMethodException nsme) {
+      LogLog.error(
+        "custom priority class [" + clazz + "]"
+        + " does not have a constructor which takes one string parameter",
nsme);
+    }
+    catch(java.lang.reflect.InvocationTargetException ite) {
+      LogLog.error(
+        "custom priority class [" + clazz + "]"
+        + " could not be instantiated", ite);
+    }
+    catch(ClassCastException cce) {
+      LogLog.error(
+        "class ["
+        + clazz
+        + "] is not a subclass of org.apache.log4j.Priority", cce);
+    }
+    catch(IllegalAccessException iae) {
+      LogLog.error(
+        "class ["
+        + clazz
+        + "] cannot be instantiated due to access restrictions", iae);
+    }
+
+    return result;
   }
 
   /**