logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Colin Sampaleanu" <colin...@exis.com>
Subject Patch (redux) modify config file loading to allow custom URL types
Date Thu, 19 Jul 2001 22:32:27 GMT
Here is another version of the code to allow custom URL types and loading of
log4j config files with them. It modifes Category.java and
OptionConverter.java to select a custom configurator class for log4j via a
log4j.configuratorClass environment variable, as opposed to the old method
of using the URL's 'ref' portion.

This is per previous discussion between Ceki and myself.

This will obviously break any old code/deployments which depend on the ref
behaviour.

I have tested this manually and all seems ok. Contrary to what the readme's
say, I was not able to run the log4j unit tests under Cygwin (but will try
again in the future), so somebody with access to a unix box should run the
tests there...

---

Index: src/java/org/apache/log4j/Category.java
===================================================================
RCS file:
/home/cvspublic/jakarta-log4j/src/java/org/apache/log4j/Category.java,v
retrieving revision 1.36
diff -u -w -r1.36 Category.java
--- src/java/org/apache/log4j/Category.java 2001/07/13 07:52:48 1.36
+++ src/java/org/apache/log4j/Category.java 2001/07/19 22:23:30
@@ -101,6 +101,27 @@
      static final public String
DEFAULT_CONFIGURATION_KEY="log4j.configuration";

   /**
+     This string constant is set to <b>log4j.configuratorClass</b>.
+
+     <p>It corresponds to name of a system property that, if set,
+     specifies the fully qualified classname of the log4j configurator
+     (a subclass of {@link org.apache.log4j.spi.Configurator}) to be used
+     to configure log4j, normally via the file or URL pointed to by
+     {@link #DEFAULT_CONFIGURATION_KEY}.
+
+     <p>Note that all property keys are case sensitive.
+
+     <p>See also the full description of <a
+     href="../../../../manual.html#defaultInit">default
+     intialization</a> procedure.
+
+     <p>If no value is specified, a default configurator class is used, as
+     described in {@link OptionConverter#selectAndConfigure}.
+
+     @since 1.1.4 */
+     static final public String
DEFAULT_CONFIGURATOR_CLASS_KEY="log4j.configuratorClass";
+
+  /**
       Setting the system property <b>log4j.defaultInitOverride</b> to
       "true" or any other value than "false" will skip default
       configuration process.
@@ -130,12 +151,15 @@
       String resource = OptionConverter.getSystemProperty(

DEFAULT_CONFIGURATION_KEY,
          DEFAULT_CONFIGURATION_FILE);
+      String configuratorClass = OptionConverter.getSystemProperty(
+          DEFAULT_CONFIGURATOR_CLASS_KEY, null);
+
       URL url = null;
       try {
- // so, resource is not a URL:
- // attempt to get the resource from the class path
  url = new URL(resource);
       } catch (MalformedURLException ex) {
+ // so, resource is not a URL:
+ // attempt to get the resource from the class path
  url = Loader.getResource(resource, Category.class);
       }

@@ -144,7 +168,7 @@
       // method.
       if(url != null) {
  LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
- OptionConverter.selectAndConfigure(url, defaultHierarchy);
+ OptionConverter.selectAndConfigure(url, configuratorClass,
defaultHierarchy);
       } else {
  LogLog.debug("Could not find resource: ["+resource+"].");
       }
Index: src/java/org/apache/log4j/helpers/OptionConverter.java
===================================================================
RCS file:
/home/cvspublic/jakarta-log4j/src/java/org/apache/log4j/helpers/OptionConver
ter.java,v
retrieving revision 1.20
diff -u -w -r1.20 OptionConverter.java
--- src/java/org/apache/log4j/helpers/OptionConverter.java 2001/07/13
07:53:53 1.20
+++ src/java/org/apache/log4j/helpers/OptionConverter.java 2001/07/19
22:23:30
@@ -399,49 +399,35 @@
   /**
      Configure log4j given a URL.

-     <p>The URL format is important. Its <em>reference</em> part is
-     taken as the class name of the configurator. For example, if you
-     invoke your application using the command line
+     The url must point to a file or resource which will be interpreted by
+     a new instance of a log4j configurator.

-     <pre>
java -Dlog4j.configuration=file:/temp/myconfig.xyz#com.myCompany.myConfigura
tor
-     </pre>
-
-     then the log4j will be configured by a new instance of
-     <code>com.myCompany.myConfigurator</code> by interpreting the
-     file referenced by <code>file:/temp/myconfig.xyz</code>.  The
-     configurator you specify <em>must</em> implement the {@link
-     Configurator} interface.
-
-     <p>If the URL has no reference part, then the {@link
-     PropertyConfigurator} will parse the URL. However, if the URL
-     ends with a ".xml" extension, then the {@link DOMConfigurator}
-     will be used to parse the URL.
-
      <p>All configurations steps are taken on the
      <code>hierarchy</code> passed as parameter.

-     @since 1.0 */
+     @param url The location of the configuration file or resource.
+     @param configuratorClass The classname, of the log4j configurator
+     which will parse the file or resource at <code>url</code>. This must
be a
+     subclass of {@link Configurator}, or null. If this value is null then
a
+     default configurator of {@link PropertyConfigurator} is used, unless
the
+     filename pointed to by <code>url</code> ends in '.xml', in which case
+     {@link DOMConfigurator} is used.
+     @param hierarchy The {@link org.apache.log4j.Hierarchy} to act on.
+
+     @since 1.1.3 */
   static
   public
-  void selectAndConfigure(URL url, Hierarchy hierarchy) {
-    String clazz = url.getRef();
+  void selectAndConfigure(URL url, String configuratorClass, Hierarchy
hierarchy) {

-    if (clazz.indexOf('.') == -1 || clazz.indexOf('/') != -1 ||
clazz.indexOf('\\') != -1) {
-      LogLog.warn("Suspicious reference in URL ["+url+
-    "] will ignore refence part assuming custom URL.");
-      clazz = null;
-    }
-
-
     Configurator configurator = null;

-    if(clazz != null) {
-      LogLog.debug("Preferred configurator class: " + clazz);
-      configurator = (Configurator) instantiateByClassName(clazz,
+    if (configuratorClass != null && configuratorClass.length() > 0) {
+      LogLog.debug("Preferred configurator class: " + configuratorClass);
+      configurator = (Configurator)
instantiateByClassName(configuratorClass,
           Configurator.class,
           null);
       if(configurator == null) {
- LogLog.error("Could not instantiate configurator ["+clazz+"].");
+ LogLog.error("Could not instantiate configurator
["+configuratorClass+"].");
  return;
       }
     } else {
@@ -460,4 +446,22 @@

     configurator.doConfigure(url, hierarchy);
   }
+
+  /**
+     Configure log4j given a URL.
+
+     Convenience method for backwards compatibility, calls {@link
+     #selectAndConfigure(URL url, String configuratorClass, Hierarchy
hierarchy)}
+     with a null configuratorClass argument, resulting in the default
+     configurator class being used.
+
+     @since 1.1.3
+   */
+
+  static
+  public
+  void selectAndConfigure(URL url, Hierarchy hierarchy) {
+    selectAndConfigure(url, null, hierarchy);
+  }
+
 }




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


Mime
View raw message