logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1519337 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java log4j-core/src/test/java/org/apache/logging/log4j/core/LateConfigTest.java src/changes/changes.xml
Date Mon, 02 Sep 2013 04:20:55 GMT
Author: rgoers
Date: Mon Sep  2 04:20:54 2013
New Revision: 1519337

URL: http://svn.apache.org/r1519337
Log:
LOG4J2-293 - Reset the Configuration if the ClassLoaderContextSelector creates a LoggerContext
without a configuration location and then is later provided one.

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LateConfigTest.java
      - copied, changed from r1519315, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/FileConfigTest.java
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java?rev=1519337&r1=1519336&r2=1519337&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java
Mon Sep  2 04:20:54 2013
@@ -28,6 +28,7 @@ import java.util.concurrent.ConcurrentMa
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.helpers.Loader;
 import org.apache.logging.log4j.core.impl.ContextAnchor;
 import org.apache.logging.log4j.core.impl.ReflectiveCallerClassUtility;
@@ -224,6 +225,13 @@ public class ClassLoaderContextSelector 
         final WeakReference<LoggerContext> r = ref.get();
         LoggerContext ctx = r.get();
         if (ctx != null) {
+            if (ctx.getConfigLocation() == null && configLocation != null) {
+                LOGGER.debug("Setting configuration to {}", configLocation);
+                ctx.setConfigLocation(configLocation);
+            } else if (ctx.getConfigLocation() != null && !ctx.getConfigLocation().equals(configLocation))
{
+                LOGGER.warn("locateContext called with URI {}. Existing LoggerContext has
URI {}", configLocation,
+                    ctx.getConfigLocation());
+            }
             return ctx;
         }
         ctx = new LoggerContext(name, null, configLocation);

Copied: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LateConfigTest.java
(from r1519315, logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/FileConfigTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LateConfigTest.java?p2=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LateConfigTest.java&p1=logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/FileConfigTest.java&r1=1519315&r2=1519337&rev=1519337&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/FileConfigTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LateConfigTest.java
Mon Sep  2 04:20:54 2013
@@ -16,15 +16,11 @@
  */
 package org.apache.logging.log4j.core;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.util.Map;
-
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
+import org.apache.logging.log4j.core.config.XMLConfiguration;
 import org.apache.logging.log4j.status.StatusLogger;
 import org.apache.logging.log4j.test.appender.ListAppender;
 import org.junit.AfterClass;
@@ -32,12 +28,19 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.File;
+import java.net.URL;
+import java.util.Map;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
 /**
  *
  */
-public class FileConfigTest {
+public class LateConfigTest {
 
-    private static final String CONFIG = "target/test-classes/log4j-test2.xml";
+    private static final String CONFIG = "target/test-classes/log4j-test1.xml";
     private static Configuration config;
     private static ListAppender app;
     private static LoggerContext ctx;
@@ -46,43 +49,23 @@ public class FileConfigTest {
 
     @BeforeClass
     public static void setupClass() {
-        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
         ctx = (LoggerContext) LogManager.getContext(false);
     }
 
-    @AfterClass
-    public static void cleanupClass() {
-        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        ctx.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
-
-    @Before
-    public void before() {
-        config = ctx.getConfiguration();
-        for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet())
{
-            if (entry.getKey().equals("List")) {
-                app = (ListAppender) entry.getValue();
-                break;
-            }
-        }
-        assertNotNull("No Appender", app);
-        app.clear();
-    }
-
     @Test
     public void testReconfiguration() throws Exception {
-        final File file = new File(CONFIG);
-        final long orig = file.lastModified();
-        final long newTime = orig + 10000;
-        file.setLastModified(newTime);
-        Thread.sleep(6000);
-        for (int i = 0; i < 17; ++i) {
-            logger.debug("Reconfigure");
-        }
         final Configuration cfg = ctx.getConfiguration();
         assertNotNull("No configuration", cfg);
-        assertTrue("Reconfiguration failed", cfg != config);
+        assertTrue("Not set to default configuration", cfg instanceof DefaultConfiguration);
+        final File file = new File(CONFIG);
+        LoggerContext loggerContext = (LoggerContext) LogManager.getContext(null, false,
file.toURI());
+        assertNotNull("No Logger Context", loggerContext);
+        final Configuration newConfig = loggerContext.getConfiguration();
+        assertTrue("Configuration not reset", cfg != newConfig);
+        assertTrue("Reconfiguration failed", newConfig instanceof XMLConfiguration);
+        ctx = (LoggerContext) LogManager.getContext(false);
+        final Configuration sameConfig = ctx.getConfiguration();
+        assertTrue("Configuration should not have been reset", newConfig == sameConfig);
     }
 }
 

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1519337&r1=1519336&r2=1519337&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Sep  2 04:20:54 2013
@@ -50,6 +50,10 @@
         Centralized reflective use of Reflection#getCallerClass and properly handled its
instability in various versions
         of Java.
       </action>
+      <action issue="LOG4J2-293" dev="rgoers" type="fix">
+        Reset the Configuration if the ClassLoaderContextSelector creates a LoggerContext
without a configuration
+        location and then is later provided one.
+      </action>
       <action issue="LOG4J2-293" dev="nickwilliams" type="fix" due-to="Abhinav Shah">
         Changed the ConfigurationFactory to recognize and properly use the classpath: URI
scheme in addition to the
         classloader: URI scheme.



Mime
View raw message