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 Ju...@stonesoft.com on 2001/11/27 11:47:13 UTC

Re: PATCH: modify config file loading to allow custom URL types

Hi all,

This message is a revival of the June thread on with the same subject. Then
a new configuration property (log4j.configuratorClass) was suggested as a
solution to the problem with custom URL formats. I very much needed this
functionality but could not find a patch that would have implemented this
proposal. So I went on and made the changes to the latest stable release
(1.1.3). Here's the patch in case anyone finds this useful.

The patch is designed to maintain full backwards compatibility. Especially
the existing behaviour of allowing one to enter the configurator class name
as the url reference part is still available.

The feature works for me, but no extensive testing has been performed. You
probably want to review this patch before applying it...

PS. I'm not subscribed on the log4j-dev mailing list and will probably not
be interested in joining. If further comments/developments on this patch
are needed, then please include me in the mail loop.

Jukka Zitting
Project Manager
Stonesoft Corp.
http://www.stonesoft.com/

diff -r -u jakarta-log4j-1.1.3.orig/src/java/org/apache/log4j/Category.java
jakarta-log4j-1.1.3/src/java/org/apache/log4j/Category.java
--- jakarta-log4j-1.1.3.orig/src/java/org/apache/log4j/Category.java   Tue
Jun 19 20:22:32 2001
+++ jakarta-log4j-1.1.3/src/java/org/apache/log4j/Category.java   Tue Nov
27 12:17:14 2001
@@ -99,6 +99,36 @@
      @since 1.0 */
      static final public String DEFAULT_CONFIGURATION_KEY
="log4j.configuration";

+
+  /**
+     This string is set to <code>null</code> to invoke the default
+     {@link PropertyConfigurator} or {@link DOMConfigurator} configurator
+     classes.
+
+     <p>See also {@link #DEFAULT_CONFIGURATOR_CLASS_KEY} for a more
general
+     alternative.
+
+     */
+     static final public String DEFAULT_CONFIGURATOR_CLASS_NAME=null;
+
+  /**
+     This string constant is set to <b>log4j.configuratorClass</b>.
+
+     <p>It corresponds to name of a system property that, if set,
+     specifies the name of the configurator class that log4j should
+     use to configure itself. See {@link
OptionConverter#selectAndConfigure}
+     for more detailed information on processing this option.
+
+     <p>Setting the <b>log4j.configuratorClass</b> system proprety
+     overrides the default use of {@link PropertyConfigurator} or
+     {@link DOMConfigurator}.
+
+     <p>Note that all property keys are case sensitive.
+
+     */
+     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
@@ -130,12 +160,14 @@

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

       // If we have a non-null url, then delegate the rest of the
@@ -143,7 +175,16 @@
       // method.
       if(url != null) {
     LogLog.debug("Using URL ["+url+"] for automatic log4j
configuration.");
-    OptionConverter.selectAndConfigure(url, defaultHierarchy);
+        String clazz = OptionConverter.getSystemProperty(
+                             DEFAULT_CONFIGURATOR_CLASS_KEY,
+                             DEFAULT_CONFIGURATOR_CLASS_NAME);
+        if (isUrl && clazz == null) {
+          // Fall back to old operation only when an configuration URL
+          // but no configurator class was given in system properties
+      OptionConverter.selectAndConfigure(url, defaultHierarchy);
+        } else {
+          OptionConverter.selectAndConfigure(url, clazz,
defaultHierarchy);
+        }
       } else {
     LogLog.debug("Could not find resource: ["+resource+"].");
       }
diff -r -u
jakarta-log4j-1.1.3.orig/src/java/org/apache/log4j/helpers/OptionConverter.java

jakarta-log4j-1.1.3/src/java/org/apache/log4j/helpers/OptionConverter.java
---
jakarta-log4j-1.1.3.orig/src/java/org/apache/log4j/helpers/OptionConverter.java

Tue Jun 19 20:22:43 2001
+++
jakarta-log4j-1.1.3/src/java/org/apache/log4j/helpers/OptionConverter.java
Tue Nov 27 11:49:16 2001
@@ -425,8 +425,32 @@
   static
   public
   void selectAndConfigure(URL url, Hierarchy hierarchy) {
-    String clazz = url.getRef();
+    selectAndConfigure(url, url.getRef(), hierarchy);
+  }

+  /**
+     Configure log4j given a URL and a configurator class name.
+
+     <p>Log4j will be configured by a new instance of the given
+     configurator class by interpreting the file referenced by
+     the given URL. The configurator you specify <em>must</em>
+     implement the {@link Configurator} interface.
+
+     <p>If the configurator class name is not given
+     (i.e. is <code>null</code>), 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.
+
+     @author based on code written by Anders Kristensen
+
+     */
+  static
+  public
+  void selectAndConfigure(URL url, String clazz, Hierarchy hierarchy) {
     Configurator configurator = null;

     if(clazz != null) {



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>