logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Colin Sampaleanu <colin...@exis.com>
Subject Re: Patch (redux) modify config file loading to allow custom URL types
Date Fri, 20 Jul 2001 19:59:48 GMT
Here is another submission, done through Eudora instead of Outlook Express 
(looks like Outlook and Eudora are ok, while Outlook Express puts in 
hard-coded line breaks)

---

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 -w -u -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/20 19:44:31
@@ -1,4 +1,8 @@
  /*
+ * $Header: $
+ *
+ * ====================================================================
+ *
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software
@@ -101,6 +105,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 +155,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 +172,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/OptionConverter.java,v
retrieving revision 1.22
diff -w -u -r1.22 OptionConverter.java
--- src/java/org/apache/log4j/helpers/OptionConverter.java	2001/07/20 
16:23:19	1.22
+++ src/java/org/apache/log4j/helpers/OptionConverter.java	2001/07/20 19:44:31
@@ -1,4 +1,8 @@
  /*
+ * $Header: $
+ *
+ * ====================================================================
+ *
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
@@ -410,43 +414,36 @@

    /**
       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
-
-     <pre> java 
-Dlog4j.configuration=file:/temp/myconfig.xyz#com.myCompany.myConfigurator
-     </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.
+     The url must point to a file or resource which will be interpreted by
+     a new instance of a log4j configurator.

       <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) {

      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 {
@@ -465,4 +462,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