logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1312071 - in /logging/log4j/trunk: src/main/java/org/apache/log4j/ src/main/java/org/apache/log4j/helpers/ src/main/java/org/apache/log4j/lf5/ src/main/java/org/apache/log4j/spi/ src/main/java/org/apache/log4j/varia/ tests/src/java/org/apa...
Date Wed, 11 Apr 2012 02:25:32 GMT
Author: ggregory
Date: Wed Apr 11 02:25:31 2012
New Revision: 1312071

URL: http://svn.apache.org/viewvc?rev=1312071&view=rev
Log:
Bugzilla issue 52913: Configure from an InputStream.

Modified:
    logging/log4j/trunk/src/main/java/org/apache/log4j/PropertyConfigurator.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/helpers/OptionConverter.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/spi/Configurator.java
    logging/log4j/trunk/src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java

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=1312071&r1=1312070&r2=1312071&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 Wed Apr 11
02:25:31 2012
@@ -405,16 +405,28 @@ public class PropertyConfigurator implem
   }
 
   /**
-     Read configuration options from url <code>configURL</code>.
+  Read configuration options from url <code>configURL</code>.
 
-     @since 0.8.2
-   */
-  public
-  static
-  void configure(java.net.URL configURL) {
-    new PropertyConfigurator().doConfigure(configURL,
-					   LogManager.getLoggerRepository());
-  }
+  @since 0.8.2
+*/
+public
+static
+void configure(java.net.URL configURL) {
+ new PropertyConfigurator().doConfigure(configURL,
+                    LogManager.getLoggerRepository());
+}
+
+/**
+Reads configuration options from an InputStream.
+
+@since 1.2.17
+*/
+public
+static
+void configure(InputStream inputStream) {
+new PropertyConfigurator().doConfigure(inputStream,
+                  LogManager.getLoggerRepository());
+}
 
 
   /**
@@ -509,6 +521,27 @@ public class PropertyConfigurator implem
     registry.clear();
   }
 
+    /**
+     * Read configuration options from url <code>configURL</code>.
+     * 
+     * @since 1.2.17
+     */
+    public void doConfigure(InputStream inputStream, LoggerRepository hierarchy) {
+        Properties props = new Properties();
+        try {
+            props.load(inputStream);
+        } catch (IOException e) {
+            if (e instanceof InterruptedIOException) {
+                Thread.currentThread().interrupt();
+            }
+            LogLog.error("Could not read configuration file from InputStream [" + inputStream
+                 + "].", e);
+            LogLog.error("Ignoring configuration InputStream [" + inputStream +"].");
+            return;
+          }
+        this.doConfigure(props, hierarchy);
+    }
+
   /**
      Read configuration options from url <code>configURL</code>.
    */

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=1312071&r1=1312070&r2=1312071&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 Wed Apr
11 02:25:31 2012
@@ -17,14 +17,15 @@
 
 package org.apache.log4j.helpers;
 
-import java.util.Properties;
-import java.net.URL;
+import java.io.InputStream;
 import java.io.InterruptedIOException;
+import java.net.URL;
+import java.util.Properties;
 
 import org.apache.log4j.Level;
+import org.apache.log4j.PropertyConfigurator;
 import org.apache.log4j.spi.Configurator;
 import org.apache.log4j.spi.LoggerRepository;
-import org.apache.log4j.PropertyConfigurator;
 
 // Contributors:   Avy Sharell (sharell@online.fr)
 //                 Matthieu Verbert (mve@zurich.ibm.com)
@@ -435,6 +436,48 @@ public class OptionConverter {
     }
   }
 
+    /**
+     * Configure log4j given an {@link InputStream}.
+     * 
+     * <p>
+     * The InputStream will be interpreted by a new instance of a log4j configurator.
+     * </p>
+     * <p>
+     * All configurations steps are taken on the <code>hierarchy</code> passed
as a parameter.
+     * </p>
+     * 
+     * @param inputStream
+     *            The configuration input stream.
+     * @param clazz
+     *            The class name, of the log4j configurator which will parse the <code>inputStream</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.
+     * @param hierarchy
+     *            The {@link org.apache.log4j.Hierarchy} to act on.
+     * @since 1.2.17
+     */
+
+static
+public
+void selectAndConfigure(InputStream inputStream, String clazz, LoggerRepository hierarchy)
{
+Configurator configurator = null;
+
+if(clazz != null) {
+  LogLog.debug("Preferred configurator class: " + clazz);
+  configurator = (Configurator) instantiateByClassName(clazz,
+                           Configurator.class,
+                           null);
+  if(configurator == null) {
+   LogLog.error("Could not instantiate configurator ["+clazz+"].");
+   return;
+  }
+} else {
+  configurator = new PropertyConfigurator();
+}
+
+configurator.doConfigure(inputStream, hierarchy);
+}
+
 
   /**
      Configure log4j given a URL.

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java?rev=1312071&r1=1312070&r2=1312071&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/lf5/DefaultLF5Configurator.java Wed
Apr 11 02:25:31 2012
@@ -17,13 +17,14 @@
 
 package org.apache.log4j.lf5;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
 import org.apache.log4j.PropertyConfigurator;
 import org.apache.log4j.spi.Configurator;
 import org.apache.log4j.spi.LoggerRepository;
 
-import java.io.IOException;
-import java.net.URL;
-
 /**
  * The <code>DefaultLF5Configurator</code> provides a default
  * configuration for the <code>LF5Appender</code>.
@@ -92,10 +93,19 @@ public class DefaultLF5Configurator impl
   /**
    * This is a dummy method that will throw an
    * <code>IllegalStateException</code> if used.
+   * 
+   * @since 1.2.17
+   */
+  public void doConfigure(InputStream inputStream, LoggerRepository repository) {
+    throw new IllegalStateException("This class should NOT be instantiated!");
+  }
+
+  /**
+   * This is a dummy method that will throw an
+   * <code>IllegalStateException</code> if used.
    */
   public void doConfigure(URL configURL, LoggerRepository repository) {
-    throw new IllegalStateException("This class should NOT be" +
-        " instantiated!");
+    throw new IllegalStateException("This class should NOT be instantiated!");
   }
 
   //--------------------------------------------------------------------------

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/spi/Configurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/spi/Configurator.java?rev=1312071&r1=1312070&r2=1312071&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/spi/Configurator.java (original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/spi/Configurator.java Wed Apr 11 02:25:31
2012
@@ -17,6 +17,7 @@
 
 package org.apache.log4j.spi;
 
+import java.io.InputStream;
 import java.net.URL;
 
 /**
@@ -42,6 +43,19 @@ public interface Configurator {
 
 
   /**
+    Interpret a resource pointed by a InputStream and set up log4j accordingly.
+
+    The configuration is done relative to the <code>hierarchy</code>
+    parameter.
+
+    @param inputStream The InputStream to parse
+    @param repository The hierarchy to operation upon.
+
+    @since 1.2.17
+   */
+  void doConfigure(InputStream inputStream, LoggerRepository repository);
+
+  /**
      Interpret a resource pointed by a URL and set up log4j accordingly.
 
      The configuration is done relative to the <code>hierarchy</code>

Modified: logging/log4j/trunk/src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java?rev=1312071&r1=1312070&r2=1312071&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java
(original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/varia/ReloadingPropertyConfigurator.java
Wed Apr 11 02:25:31 2012
@@ -17,22 +17,27 @@
 
 package org.apache.log4j.varia;
 
+import java.io.InputStream;
+import java.net.URL;
+
 import org.apache.log4j.PropertyConfigurator;
 import org.apache.log4j.spi.Configurator;
-import java.net.URL;
-import  org.apache.log4j.spi.LoggerRepository;
+import org.apache.log4j.spi.LoggerRepository;
 
 public class ReloadingPropertyConfigurator implements Configurator {
 
+    PropertyConfigurator delegate = new PropertyConfigurator();
+
+    public ReloadingPropertyConfigurator() {
+    }
 
-  PropertyConfigurator delegate = new PropertyConfigurator();
+   /**
+    * @since 1.2.17
+    */
+    public void doConfigure(InputStream inputStream, LoggerRepository repository) {
+    }
 
-  
-  public ReloadingPropertyConfigurator() {    
-  }
-
-  public
-  void doConfigure(URL url, LoggerRepository repository) {
-  }
+    public void doConfigure(URL url, LoggerRepository repository) {
+    }
 
 }

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java?rev=1312071&r1=1312070&r2=1312071&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java Wed
Apr 11 02:25:31 2012
@@ -16,9 +16,8 @@
  */
 package org.apache.log4j;
 
-import junit.framework.TestCase;
-
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -27,9 +26,11 @@ import java.util.Properties;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
-import org.apache.log4j.spi.OptionHandler;
+import junit.framework.TestCase;
+
 import org.apache.log4j.spi.Filter;
 import org.apache.log4j.spi.LoggingEvent;
+import org.apache.log4j.spi.OptionHandler;
 import org.apache.log4j.spi.ThrowableRenderer;
 import org.apache.log4j.spi.ThrowableRendererSupport;
 import org.apache.log4j.varia.LevelRangeFilter;
@@ -94,6 +95,39 @@ public class PropertyConfiguratorTest ex
     }
 
     /**
+     * Tests configuring Log4J from an InputStream.
+     * 
+     * @since 1.2.17
+     */
+    public void testInputStream() throws IOException {
+        File file = new File("input/filter1.properties");
+        assertTrue(file.exists());
+        FileInputStream inputStream = new FileInputStream(file);
+        try {
+            PropertyConfigurator.configure(inputStream);
+        } finally {
+            inputStream.close();
+        }
+        this.validateNested();
+        LogManager.resetConfiguration();
+    }
+
+    public void validateNested() {
+        RollingFileAppender rfa = (RollingFileAppender)
+                Logger.getLogger("org.apache.log4j.PropertyConfiguratorTest")
+                   .getAppender("ROLLING");
+        FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) rfa.getRollingPolicy();
+        assertEquals("filterBase-test1.log", rollingPolicy.getActiveFileName());
+        assertEquals("filterBased-test1.%i", rollingPolicy.getFileNamePattern());
+        assertEquals(0, rollingPolicy.getMinIndex());
+        assertTrue(rollingPolicy.isActivated());
+        FilterBasedTriggeringPolicy triggeringPolicy =
+                (FilterBasedTriggeringPolicy) rfa.getTriggeringPolicy();
+        LevelRangeFilter filter = (LevelRangeFilter) triggeringPolicy.getFilter();
+        assertTrue(Level.INFO.equals(filter.getLevelMin()));        
+    }
+    
+    /**
      * Test for bug 47465.
      * configure(URL) did not close opened JarURLConnection.
      * @throws IOException if IOException creating properties jar.
@@ -274,24 +308,15 @@ public class PropertyConfiguratorTest ex
     }
 
     /**
-     * Test processing of nested objects, see bug 36384.
-     *
+     * Tests processing of nested objects, see bug 36384.
      */
     public void testNested() {
-        PropertyConfigurator.configure("input/filter1.properties");
-        RollingFileAppender rfa = (RollingFileAppender)
-                Logger.getLogger("org.apache.log4j.PropertyConfiguratorTest")
-                   .getAppender("ROLLING");
-        FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) rfa.getRollingPolicy();
-        assertEquals("filterBase-test1.log", rollingPolicy.getActiveFileName());
-        assertEquals("filterBased-test1.%i", rollingPolicy.getFileNamePattern());
-        assertEquals(0, rollingPolicy.getMinIndex());
-        assertTrue(rollingPolicy.isActivated());
-        FilterBasedTriggeringPolicy triggeringPolicy =
-                (FilterBasedTriggeringPolicy) rfa.getTriggeringPolicy();
-        LevelRangeFilter filter = (LevelRangeFilter) triggeringPolicy.getFilter();
-        assertTrue(Level.INFO.equals(filter.getLevelMin()));
-        LogManager.resetConfiguration();
+        try {
+            PropertyConfigurator.configure("input/filter1.properties");
+            this.validateNested();
+        } finally {
+            LogManager.resetConfiguration();
+        }
     }
 
 

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java?rev=1312071&r1=1312070&r2=1312071&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java
(original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/helpers/OptionConverterTestCase.java
Wed Apr 11 02:25:31 2012
@@ -20,13 +20,19 @@
 
 package org.apache.log4j.helpers;
 
-import org.apache.log4j.Level;
-import org.apache.log4j.xml.XLevel;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Properties;
 
+import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
-import junit.framework.Test;
-import java.util.Properties;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.PropertyConfiguratorTest;
+import org.apache.log4j.xml.XLevel;
 
 /**
    Test variable substitution code.   
@@ -48,6 +54,10 @@ public class OptionConverterTestCase ext
     props.put("TOTO", "wonderful");
     props.put("key1", "value1");
     props.put("key2", "value2");
+    // Log4J will NPE without this:
+    props.put("line.separator", System.getProperty("line.separator"));
+    // Log4J will throw an Error without this:
+    props.put("java.home", System.getProperty("java.home"));
     System.setProperties(props);
 
 
@@ -56,6 +66,7 @@ public class OptionConverterTestCase ext
   public
   void tearDown() {
     props = null;
+    LogManager.resetConfiguration();
   }
 
   public
@@ -113,6 +124,23 @@ public class OptionConverterTestCase ext
     assertEquals(res, "x1");
   }
 
+  /**
+   * Tests configuring Log4J from an InputStream.
+   * 
+   * @since 1.2.17
+   */
+    public void testInputStream() throws IOException {
+        File file = new File("input/filter1.properties");
+        assertTrue(file.exists());
+        FileInputStream inputStream = new FileInputStream(file);
+        try {
+            OptionConverter.selectAndConfigure(inputStream, null, LogManager.getLoggerRepository());
+        } finally {
+            inputStream.close();
+        }
+        new PropertyConfiguratorTest(this.getClass().getName()).validateNested();
+    }
+
   public
   void toLevelTest1() {
     String val = "INFO";
@@ -148,7 +176,6 @@ public class OptionConverterTestCase ext
     assertEquals(p, null);
   }
 
-
   public
   static
   Test suite() {
@@ -159,6 +186,7 @@ public class OptionConverterTestCase ext
     suite.addTest(new OptionConverterTestCase("varSubstTest3"));
     suite.addTest(new OptionConverterTestCase("varSubstTest4"));
 
+    suite.addTest(new OptionConverterTestCase("testInputStream"));
 
     suite.addTest(new OptionConverterTestCase("toLevelTest1"));
     suite.addTest(new OptionConverterTestCase("toLevelTest2"));



Mime
View raw message