You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2019/06/18 04:02:21 UTC

[logging-log4j2] branch master updated: LOG4J2-2406 - Add reconfiguration methods to Configurator

This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new c334026  LOG4J2-2406 - Add reconfiguration methods to Configurator
c334026 is described below

commit c334026e4f4274f3db649a05dbfa099f01cd47e4
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Jun 17 21:01:54 2019 -0700

    LOG4J2-2406 - Add reconfiguration methods to Configurator
---
 .../logging/log4j/core/config/Configurator.java    | 35 ++++++++++++++++++++++
 .../log4j/core/config/ConfiguratorTest.java        | 29 ++++++++++++++++++
 src/changes/changes.xml                            |  3 ++
 3 files changed, 67 insertions(+)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
index 28dd85f..30f8ca8 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
@@ -227,6 +227,41 @@ public final class Configurator {
     }
 
     /**
+     * Reload the existing reconfiguration.
+     * @since 2.12.0
+     */
+    public static void reconfigure() {
+        try {
+            Log4jContextFactory factory = getFactory();
+            if (factory != null) {
+                factory.getSelector().getContext(FQCN, null, false).reconfigure();
+            } else {
+                LOGGER.warn("Unable to reconfigure - Log4j has not been initialized.");
+            }
+        } catch (final Exception ex) {
+            LOGGER.error("Error encountered trying to reconfigure logging", ex);
+        }
+    }
+
+    /**
+     * Reconfigure with a potentially new configuration.
+     * @param uri The location of the configuration.
+     * @since 2.12.0
+     */
+    public static void reconfigure(final URI uri) {
+        try {
+            Log4jContextFactory factory = getFactory();
+            if (factory != null) {
+                factory.getSelector().getContext(FQCN, null, false).setConfigLocation(uri);
+            } else {
+                LOGGER.warn("Unable to reconfigure - Log4j has not been initialized.");
+            }
+        } catch (final Exception ex) {
+            LOGGER.error("Error encountered trying to reconfigure logging", ex);
+        }
+    }
+
+    /**
      * Sets the levels of <code>parentLogger</code> and all 'child' loggers to the given <code>level</code>.
      * @param parentLogger the parent logger
      * @param level the new level
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
index 8a40d22..485c8cd 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfiguratorTest.java
@@ -17,11 +17,15 @@
 package org.apache.logging.log4j.core.config;
 
 import java.io.File;
+import java.net.URI;
 
 import org.apache.logging.log4j.core.LoggerContext;
 import org.junit.Assert;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
 public class ConfiguratorTest {
 
     @Test
@@ -36,6 +40,31 @@ public class ConfiguratorTest {
         testInitializeFromFilePath(path);
     }
 
+    @Test
+    public void testReconfigure() {
+        final String path = new File("src/test/resources/log4j-list.xml").getAbsolutePath();
+        try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) {
+            assertNotNull(loggerContext.getConfiguration().getAppender("List"));
+            URI uri = loggerContext.getConfigLocation();
+            assertNotNull("No configuration location returned", uri);
+            Configurator.reconfigure();
+            assertEquals("Unexpected configuration location returned", uri, loggerContext.getConfigLocation());
+        }
+    }
+
+    @Test
+    public void testReconfigureFromPath() {
+        final String path = new File("src/test/resources/log4j-list.xml").getAbsolutePath();
+        try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) {
+            assertNotNull(loggerContext.getConfiguration().getAppender("List"));
+            URI uri = loggerContext.getConfigLocation();
+            assertNotNull("No configuration location returned", uri);
+            final URI location = new File("src/test/resources/log4j2-config.xml").toURI();
+            Configurator.reconfigure(location);
+            assertEquals("Unexpected configuration location returned", location, loggerContext.getConfigLocation());
+        }
+    }
+
     private void testInitializeFromFilePath(final String path) {
         try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) {
             Assert.assertNotNull(loggerContext.getConfiguration().getAppender("List"));
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 72d34f3..60f782f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -142,6 +142,9 @@
       </action>
     </release>
     <release version="2.12.0" date="2019-MM-DD" description="GA Release 2.12.0">
+      <action issue="LOG4J2-2406" dev="rgoers" type="add">
+        Add reconfiguration methods to Configurator.
+      </action>
       <action issue="LOG4J2-1852" dev="rgoers" type="fix" due-to="Tanner Altares">
         Locate plugins within a Jar using a URL Connection.
       </action>