This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 4c70958 LOG4J2-2893 - Allow reconfiguration when Log4j 1
configuration files are updated.
4c70958 is described below
commit 4c70958d705691b83530ad63d07dce797b44f535
Author: Ralph Goers <[email protected]>
AuthorDate: Sun Feb 28 00:50:39 2021 -0700
LOG4J2-2893 - Allow reconfiguration when Log4j 1 configuration files are
updated.
---
.../log4j/config/PropertiesConfiguration.java | 19 +++++
.../org/apache/log4j/xml/XmlConfiguration.java | 20 +++++
.../config/PropertiesReconfigurationTest.java | 95 +++++++++++++++++++++
.../log4j/config/XmlReconfigurationTest.java | 96 ++++++++++++++++++++++
src/changes/changes.xml | 3 +
5 files changed, 233 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 5b498da..f20d26c 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
@@ -26,11 +26,13 @@ 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.util.LoaderUtil;
+import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
@@ -90,6 +92,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 89dc460..2a9de0b 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
@@ -22,6 +22,8 @@ import org.apache.log4j.Level;
import org.apache.log4j.bridge.AppenderAdapter;
import org.apache.log4j.bridge.AppenderWrapper;
import org.apache.log4j.config.Log4j1Configuration;
+import org.apache.log4j.config.PropertiesConfiguration;
+import org.apache.log4j.config.PropertiesConfigurationFactory;
import org.apache.log4j.config.PropertySetter;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.rewrite.RewritePolicy;
@@ -29,6 +31,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;
@@ -171,6 +174,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 dc548d2..7c0b068 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -94,6 +94,9 @@
Support stack trace truncation in JsonTemplateLayout.
</action>
<!-- UPDATES -->
+ <action issue="LOG4J2-2893" dev="rgoers" type="update">
+ Allow reconfiguration when Log4j 1 configuration files are updated.
+ </action>
<action dev="rgoers" type="update">
Update Spring dependencies to 5.3.2, Spring Boot to 2.3.6, and Spring
Cloud to Hoxton.SR9
</action>