logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1400365 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/config/ core/src/test/java/org/apache/logging/log4j/core/config/ src/changes/
Date Sat, 20 Oct 2012 04:47:33 GMT
Author: rgoers
Date: Sat Oct 20 04:47:32 2012
New Revision: 1400365

URL: http://svn.apache.org/viewvc?rev=1400365&view=rev
Log:
LOG4J2-55 - Added ability to configure from an InputSource.

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java?rev=1400365&r1=1400364&r2=1400365&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
Sat Oct 20 04:47:32 2012
@@ -418,6 +418,26 @@ public abstract class ConfigurationFacto
 
         @Override
         public Configuration getConfiguration(InputSource source) {
+            if (source != null) {
+                String config = source.getSystemId() != null ? source.getSystemId() : source.getPublicId();
+                for (ConfigurationFactory factory : factories) {
+                    String[] types = factory.getSupportedTypes();
+                    if (types != null) {
+                        for (String type : types) {
+                            if (type.equals("*") || (config != null && config.endsWith(type)))
{
+                                Configuration c = factory.getConfiguration(source);
+                                if (c != null) {
+                                    return c;
+                                } else {
+                                    LOGGER.error("Cannot determine the ConfigurationFactory
to use for {}", config);
+                                    return null;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            LOGGER.error("Cannot process configuration, input source is null");
             return null;
         }
     }

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java?rev=1400365&r1=1400364&r2=1400365&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
Sat Oct 20 04:47:32 2012
@@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.co
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.xml.sax.InputSource;
 
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -68,6 +69,25 @@ public final class Configurator {
         return null;
     }
 
+    /**
+     * Initialize the Logging Context.
+     * @param loader The ClassLoader for the Context (or null).
+     * @param source The InputSource for the configuration.
+     * @return The LoggerContext.
+     */
+    public static LoggerContext initialize(ClassLoader loader, InputSource source) {
+
+        try {
+            LoggerContext ctx = (LoggerContext) LogManager.getContext(loader, false);
+            Configuration config = ConfigurationFactory.getInstance().getConfiguration(source);
+            ctx.setConfiguration(config);
+            return ctx;
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        }
+        return null;
+    }
+
     public static void shutdown(LoggerContext ctx) {
         if (ctx != null) {
             ctx.setConfiguration(new DefaultConfiguration());

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java?rev=1400365&r1=1400364&r2=1400365&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
Sat Oct 20 04:47:32 2012
@@ -23,8 +23,11 @@ import org.apache.logging.log4j.core.Log
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.xml.sax.InputSource;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.util.Map;
 
 import static org.junit.Assert.assertTrue;
@@ -57,6 +60,45 @@ public class TestConfigurator {
     }
 
     @Test
+    public void testFromStream() throws Exception {
+        InputStream is = new FileInputStream("target/test-classes/log4j2-config.xml");
+        InputSource source = new InputSource(is);
+        source.setSystemId("target/test-classes/log4j2-config.xml");
+        LoggerContext ctx = Configurator.initialize(null, source);
+        Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
+        Configuration config = ctx.getConfiguration();
+        assertNotNull("No configuration", config);
+        assertTrue("Incorrect Configuration. Expected " + CONFIG_NAME + " but found " + config.getName(),
+            CONFIG_NAME.equals(config.getName()));
+        Map<String, Appender> map = config.getAppenders();
+        assertNotNull("No Appenders", map != null && map.size() > 0);
+        assertTrue("Wrong configuration", map.containsKey("List"));
+        Configurator.shutdown(ctx);
+        config = ctx.getConfiguration();
+        assertTrue("Incorrect Configuration. Expected " + DefaultConfiguration.DEFAULT_NAME
+ " but found " +
+            config.getName(), DefaultConfiguration.DEFAULT_NAME.equals(config.getName()));
+    }
+
+    @Test
+    public void testFromStreamNoId() throws Exception {
+        InputStream is = new FileInputStream("target/test-classes/log4j2-config.xml");
+        InputSource source = new InputSource(is);
+        LoggerContext ctx = Configurator.initialize(null, source);
+        Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
+        Configuration config = ctx.getConfiguration();
+        assertNotNull("No configuration", config);
+        assertTrue("Incorrect Configuration. Expected " + CONFIG_NAME + " but found " + config.getName(),
+            CONFIG_NAME.equals(config.getName()));
+        Map<String, Appender> map = config.getAppenders();
+        assertNotNull("No Appenders", map != null && map.size() > 0);
+        assertTrue("Wrong configuration", map.containsKey("List"));
+        Configurator.shutdown(ctx);
+        config = ctx.getConfiguration();
+        assertTrue("Incorrect Configuration. Expected " + DefaultConfiguration.DEFAULT_NAME
+ " but found " +
+            config.getName(), DefaultConfiguration.DEFAULT_NAME.equals(config.getName()));
+    }
+
+    @Test
     public void testFromClassPath() throws Exception {
         LoggerContext ctx = Configurator.initialize("Test1", null, "log4j2-config.xml");
         Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1400365&r1=1400364&r2=1400365&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat Oct 20 04:47:32 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.0-beta3" date="TBD" description= "Bug fixes and enhancements">
+      <action issue="LOG4J2-55" dev="rgoers" type="add">
+        Added ability to configure from an InputSource.
+      </action>
       <action issue="LOG4J2-102" dev="rgoers" type="fix" due-to="Emanuele Colombo">
         The Facility value was being improperly calculated.
       </action>



Mime
View raw message