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 6454ff6  LOG4J2-2893 - Allow reconfiguration when Log4j 1 
configuration files are updated.
6454ff6 is described below

commit 6454ff6dbafe9693071f82390b1bb0689e017656
Author: Ralph Goers <[email protected]>
AuthorDate: Sun Feb 28 00:48:49 2021 -0700

    LOG4J2-2893 - Allow reconfiguration when Log4j 1 configuration files are 
updated.
---
 .../log4j/config/PropertiesConfiguration.java      | 19 +++++
 .../org/apache/log4j/xml/XmlConfiguration.java     | 18 ++++
 .../config/PropertiesReconfigurationTest.java      | 95 +++++++++++++++++++++
 .../log4j/config/XmlReconfigurationTest.java       | 96 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 +
 5 files changed, 231 insertions(+)

diff --git 
a/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
 
b/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
index 2ceffaf..5e0a946 100644
--- 
a/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
+++ 
b/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
@@ -27,12 +27,14 @@ import org.apache.log4j.helpers.OptionConverter;
 import org.apache.log4j.spi.ErrorHandler;
 import org.apache.log4j.spi.Filter;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationSource;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.core.config.status.StatusConfiguration;
 import org.apache.logging.log4j.core.filter.AbstractFilterable;
 import org.apache.logging.log4j.util.LoaderUtil;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
@@ -91,6 +93,23 @@ public class PropertiesConfiguration  extends 
Log4j1Configuration {
         doConfigure(props);
     }
 
+    @Override
+    public Configuration reconfigure() {
+        try {
+            final ConfigurationSource source = 
getConfigurationSource().resetInputStream();
+            if (source == null) {
+                return null;
+            }
+            final PropertiesConfigurationFactory factory = new 
PropertiesConfigurationFactory();
+            final PropertiesConfiguration config =
+                    (PropertiesConfiguration) 
factory.getConfiguration(getLoggerContext(), source);
+            return config == null || config.getState() != State.INITIALIZING ? 
null : config;
+        } catch (final IOException ex) {
+            LOGGER.error("Cannot locate file {}: {}", 
getConfigurationSource(), ex);
+        }
+        return null;
+    }
+
     /**
      * Read configuration from a file. <b>The existing configuration is
      * not cleared nor reset.</b> If you require a different behavior,
diff --git 
a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java 
b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
index 8e256bc..2b3e76f 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfiguration.java
@@ -29,6 +29,7 @@ import org.apache.log4j.spi.AppenderAttachable;
 import org.apache.log4j.spi.ErrorHandler;
 import org.apache.log4j.spi.Filter;
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationSource;
 import org.apache.logging.log4j.core.config.LoggerConfig;
 import org.apache.logging.log4j.core.config.status.StatusConfiguration;
@@ -167,6 +168,23 @@ public class XmlConfiguration extends Log4j1Configuration {
         }
     }
 
+    @Override
+    public Configuration reconfigure() {
+        try {
+            final ConfigurationSource source = 
getConfigurationSource().resetInputStream();
+            if (source == null) {
+                return null;
+            }
+            final XmlConfigurationFactory factory = new 
XmlConfigurationFactory();
+            final XmlConfiguration config =
+                    (XmlConfiguration) 
factory.getConfiguration(getLoggerContext(), source);
+            return config == null || config.getState() != State.INITIALIZING ? 
null : config;
+        } catch (final IOException ex) {
+            LOGGER.error("Cannot locate file {}: {}", 
getConfigurationSource(), ex);
+        }
+        return null;
+    }
+
     /**
      * Delegates unrecognized content to created instance if it supports 
UnrecognizedElementParser.
      *
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesReconfigurationTest.java
 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesReconfigurationTest.java
new file mode 100644
index 0000000..13e0035
--- /dev/null
+++ 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesReconfigurationTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.log4j.config;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationListener;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.config.Reconfigurable;
+import org.apache.logging.log4j.spi.LoggerContextFactory;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Test reconfiguring with an XML configuration.
+ */
+public class PropertiesReconfigurationTest {
+
+    private static final String CONFIG = 
"target/test-classes/log4j1-file.properties";
+    private static final long FIVE_MINUTES = 5 * 60 * 1000;
+
+    private CountDownLatch toggle = new CountDownLatch(1);
+
+    @Test
+    public void testReconfiguration() throws Exception {
+        System.setProperty(Log4j1Configuration.MONITOR_INTERVAL, "1");
+        File file = new File(CONFIG);
+        assertNotNull("No Config file", file);
+        long configMillis = file.lastModified();
+        assertTrue("Unable to modified file time", 
file.setLastModified(configMillis - FIVE_MINUTES));
+        LoggerContext context = configure(file);
+        Logger logger = LogManager.getLogger("test");
+        logger.info("Hello");
+        Configuration original = context.getConfiguration();
+        TestListener listener = new TestListener();
+        original.addListener(listener);
+        file.setLastModified(System.currentTimeMillis());
+        try {
+            if (!toggle.await(3, TimeUnit.SECONDS)) {
+                fail("Reconfiguration timed out");
+            }
+            // Allow reconfiguration to complete.
+            Thread.sleep(500);
+        } catch (InterruptedException ie) {
+            fail("Reconfiguration interupted");
+        }
+        Configuration updated = context.getConfiguration();
+        assertTrue("Configurations are the same", original != updated);
+    }
+
+    private class TestListener implements ConfigurationListener {
+
+        public synchronized void onChange(final Reconfigurable reconfigurable) 
{
+            toggle.countDown();
+        }
+
+    }
+
+    private LoggerContext configure(File configFile) throws Exception {
+        InputStream is = new FileInputStream(configFile);
+        ConfigurationSource source = new ConfigurationSource(is, configFile);
+        LoggerContextFactory factory = 
org.apache.logging.log4j.LogManager.getFactory();
+        LoggerContext context = (LoggerContext) 
org.apache.logging.log4j.LogManager.getContext(false);
+        Configuration configuration = new 
PropertiesConfigurationFactory().getConfiguration(context, source);
+        assertNotNull("No configuration created", configuration);
+        Configurator.reconfigure(configuration);
+        return context;
+    }
+}
diff --git 
a/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlReconfigurationTest.java
 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlReconfigurationTest.java
new file mode 100644
index 0000000..e657f0c
--- /dev/null
+++ 
b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlReconfigurationTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.log4j.config;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.xml.XmlConfigurationFactory;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationListener;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.config.Reconfigurable;
+import org.apache.logging.log4j.spi.LoggerContextFactory;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Test reconfiguring with an XML configuration.
+ */
+public class XmlReconfigurationTest {
+
+    private static final String CONFIG = "target/test-classes/log4j1-file.xml";
+    private static final long FIVE_MINUTES = 5 * 60 * 1000;
+
+    private CountDownLatch toggle = new CountDownLatch(1);
+
+    @Test
+    public void testReconfiguration() throws Exception {
+        System.setProperty(Log4j1Configuration.MONITOR_INTERVAL, "1");
+        File file = new File(CONFIG);
+        assertNotNull("No Config file", file);
+        long configMillis = file.lastModified();
+        assertTrue("Unable to modified file time", 
file.setLastModified(configMillis - FIVE_MINUTES));
+        LoggerContext context = configure(file);
+        Logger logger = LogManager.getLogger("test");
+        logger.info("Hello");
+        Configuration original = context.getConfiguration();
+        TestListener listener = new TestListener();
+        original.addListener(listener);
+        file.setLastModified(System.currentTimeMillis());
+        try {
+            if (!toggle.await(3, TimeUnit.SECONDS)) {
+                fail("Reconfiguration timed out");
+            }
+            // Allow reconfiguration to complete.
+            Thread.sleep(500);
+        } catch (InterruptedException ie) {
+            fail("Reconfiguration interupted");
+        }
+        Configuration updated = context.getConfiguration();
+        assertTrue("Configurations are the same", original != updated);
+    }
+
+    private class TestListener implements ConfigurationListener {
+
+        public synchronized void onChange(final Reconfigurable reconfigurable) 
{
+            toggle.countDown();
+        }
+
+    }
+
+    private LoggerContext configure(File configFile) throws Exception {
+        InputStream is = new FileInputStream(configFile);
+        ConfigurationSource source = new ConfigurationSource(is, configFile);
+        LoggerContextFactory factory = 
org.apache.logging.log4j.LogManager.getFactory();
+        LoggerContext context = (LoggerContext) 
org.apache.logging.log4j.LogManager.getContext(false);
+        Configuration configuration = new 
XmlConfigurationFactory().getConfiguration(context, source);
+        assertNotNull("No configuration created", configuration);
+        Configurator.reconfigure(configuration);
+        return context;
+    }
+}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9d2f6f0..8b6f0f2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -227,6 +227,9 @@
       </action>
     </release>
     <release version="2.14.1" date="2020-MM-DD" description="GA Release 
2.14.1">
+      <action issue="LOG4J2-2893" dev="rgoers" type="update">
+        Allow reconfiguration when Log4j 1 configuration files are updated.
+      </action>
       <action issue="LOG4J2-2990" dev="rgoers" type="fix" due-to="Diogo 
Monteiro">
         Reduce garbage by using putAll when copying the ThreadContext for 
SLF4J.
       </action>

Reply via email to