logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1521952 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java src/changes/changes.xml
Date Wed, 11 Sep 2013 17:51:04 GMT
Author: rgoers
Date: Wed Sep 11 17:51:04 2013
New Revision: 1521952

URL: http://svn.apache.org/r1521952
Log:
LOG4J2-395 - Allow classpath scheme when specifying configuration file location as a system
property.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
    logging/log4j/log4j2/trunk/log4j-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/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java?rev=1521952&r1=1521951&r2=1521952&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
Wed Sep 11 17:51:04 2013
@@ -37,6 +37,8 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.plugins.PluginType;
 import org.apache.logging.log4j.core.helpers.FileUtils;
 import org.apache.logging.log4j.core.helpers.Loader;
+import org.apache.logging.log4j.core.lookup.Interpolator;
+import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.util.PropertiesUtil;
 
@@ -104,6 +106,8 @@ public abstract class ConfigurationFacto
 
     private static ConfigurationFactory configFactory = new Factory();
 
+    protected final StrSubstitutor substitutor = new StrSubstitutor(new Interpolator());
+
     /**
      * Returns the ConfigurationFactory.
      * @return the ConfigurationFactory.
@@ -362,10 +366,19 @@ public abstract class ConfigurationFacto
         public Configuration getConfiguration(final String name, final URI configLocation)
{
 
             if (configLocation == null) {
-                final String config = PropertiesUtil.getProperties().getStringProperty(CONFIGURATION_FILE_PROPERTY);
+                final String config = this.substitutor.replace(
+                    PropertiesUtil.getProperties().getStringProperty(CONFIGURATION_FILE_PROPERTY));
                 if (config != null) {
-                    final ClassLoader loader = this.getClass().getClassLoader();
-                    final ConfigurationSource source = getInputFromString(config, loader);
+                    ConfigurationSource source = null;
+                    try {
+                        source = getInputFromURI(new URI(config));
+                    } catch (Exception ex) {
+                        // Ignore the error and try as a String.
+                    }
+                    if (source == null) {
+                        final ClassLoader loader = this.getClass().getClassLoader();
+                        source = getInputFromString(config, loader);
+                    }
                     if (source != null) {
                         for (final ConfigurationFactory factory : factories) {
                             final String[] types = factory.getSupportedTypes();

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java?rev=1521952&r1=1521951&r2=1521952&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
Wed Sep 11 17:51:04 2013
@@ -25,6 +25,7 @@ import org.apache.logging.log4j.core.Lay
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.filter.CompositeFilter;
 import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.junit.After;
 import org.junit.Test;
 
 import java.io.File;
@@ -60,6 +61,11 @@ public class TestConfigurator {
         "mmmmmmmmmm",
     };
 
+    @After
+    public void cleanup() {
+        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+    }
+
 
     @Test
     public void testFromFile() throws Exception {
@@ -132,6 +138,23 @@ public class TestConfigurator {
     }
 
     @Test
+    public void testFromClassPathProperty() throws Exception {
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "classpath:log4j2-config.xml");
+        final LoggerContext ctx = Configurator.initialize("Test1", null);
+        LogManager.getLogger("org.apache.test.TestConfigurator");
+        Configuration config = ctx.getConfiguration();
+        assertNotNull("No configuration", config);
+        assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+        final Map<String, Appender> map = config.getAppenders();
+        assertNotNull("Appenders map should not be null.", map);
+        assertTrue("Appenders map should not be empty.", map.size() > 0);
+        assertTrue("Wrong configuration", map.containsKey("List"));
+        Configurator.shutdown(ctx);
+        config = ctx.getConfiguration();
+        assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
+    }
+
+    @Test
     public void testFromClassPathWithClassPathPrefix() throws Exception {
         final LoggerContext ctx = Configurator.initialize("Test1", "classpath:log4j2-config.xml");
         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=1521952&r1=1521951&r2=1521952&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Wed Sep 11 17:51:04 2013
@@ -40,6 +40,9 @@
         reversed (previous "true"s should become "false"s, and vice versa). Since this was
an undocumented attribute up
         until now, it's unlikely this change will affect any users.
       </action>
+      <action issue="LOG4J2-395" dev="rgoers" type="fix" due-to="Abhinav Shah">
+        Allow classpath scheme when specifying configuration file location as a system property.
+      </action>
       <action issue="LOG4J2-393" dev="rgoers" type="fix">
         Initialize PluginManager once during configuration. Move advertisement setup into
BaseConfiguration.
       </action>



Mime
View raw message