Repository: logging-log4j2 Updated Branches: refs/heads/LOG4J2-1121A [created] 3c8fc8c32
Change to update LoggerConfig if a new one is created Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/3c8fc8c3 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/3c8fc8c3 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/3c8fc8c3 Branch: refs/heads/LOG4J2-1121A Commit: 3c8fc8c321540951a31725fe6294b7e90b33650c Parents: 3ecee80 Author: Ralph Goers <[email protected]> Authored: Fri Sep 18 17:19:24 2015 -0700 Committer: Ralph Goers <[email protected]> Committed: Fri Sep 18 17:19:24 2015 -0700 ---------------------------------------------------------------------- .../logging/log4j/core/LoggerContext.java | 4 +++ .../core/config/AbstractConfiguration.java | 27 +++++++++++---- .../log4j/core/config/Configuration.java | 17 +++++++--- .../logging/log4j/core/config/LoggerConfig.java | 35 ++++++++++++++------ 4 files changed, 62 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3c8fc8c3/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java index 0722323..d6ff89c 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java @@ -163,6 +163,7 @@ public class LoggerContext extends AbstractLifeCycle implements org.apache.loggi this.name = name; this.externalContext = externalContext; this.configLocation = configLocn; + this.config.setLoggerContext(this); } /** @@ -176,6 +177,7 @@ public class LoggerContext extends AbstractLifeCycle implements org.apache.loggi public LoggerContext(final String name, final Object externalContext, final String configLocn) { this.name = name; this.externalContext = externalContext; + this.config.setLoggerContext(this); if (configLocn != null) { URI uri; try { @@ -281,6 +283,7 @@ public class LoggerContext extends AbstractLifeCycle implements org.apache.loggi } final Configuration prev = config; config = NULL_CONFIGURATION; + config.setLoggerContext(this); updateLoggers(); prev.stop(); externalContext = null; @@ -410,6 +413,7 @@ public class LoggerContext extends AbstractLifeCycle implements org.apache.loggi try { final Configuration prev = this.config; config.addListener(this); + config.setLoggerContext(this); final ConcurrentMap<String, String> map = config.getComponent(Configuration.CONTEXT_PROPERTIES); try { // LOG4J2-719 network access may throw android.os.NetworkOnMainThreadException http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3c8fc8c3/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java index 3614494..3c08cb3 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java @@ -35,10 +35,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.core.Appender; -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.Layout; -import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.*; import org.apache.logging.log4j.core.appender.AsyncAppender; import org.apache.logging.log4j.core.appender.ConsoleAppender; import org.apache.logging.log4j.core.async.AsyncLoggerConfig; @@ -108,6 +105,7 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement protected final List<String> pluginPackages = new ArrayList<>(); protected PluginManager pluginManager; private final ConfigurationSource configurationSource; + private LoggerContext loggerContext = null; /** * Constructor. @@ -135,6 +133,16 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement return properties; } + @Override + public LoggerContext getLoggerContext() { + return loggerContext; + } + + @Override + public void setLoggerContext(LoggerContext loggerContext) { + this.loggerContext = loggerContext; + } + /** * Initialize the configuration. */ @@ -214,6 +222,8 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement // similarly, first stop AsyncLoggerConfig Disruptor thread(s) final Set<LoggerConfig> alreadyStopped = new HashSet<>(); int asyncLoggerConfigCount = 0; + Configuration newConfiguration = loggerContext.getConfiguration(); + for (final LoggerConfig logger : loggers.values()) { if (logger instanceof AsyncLoggerConfig) { // LOG4J2-520, LOG4J2-392: @@ -221,12 +231,14 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement // have been stopped! Stopping the last AsyncLoggerConfig will // shut down the disruptor and wait for all enqueued events to be processed. // Only *after this* the appenders can be cleared or events will be lost. + logger.setNewConfig(newConfiguration.getLoggerConfig(logger.getName())); logger.stop(); asyncLoggerConfigCount++; alreadyStopped.add(logger); } } if (root instanceof AsyncLoggerConfig & !alreadyStopped.contains(root)) { // LOG4J2-807 + root.setNewConfig(newConfiguration.getRootLogger()); root.stop(); asyncLoggerConfigCount++; alreadyStopped.add(root); @@ -257,17 +269,17 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement int loggerCount = 0; for (final LoggerConfig logger : loggers.values()) { - // clear appenders, even if this logger is already stopped. - logger.clearAppenders(); - // AsyncLoggerConfigHelper decreases its ref count when an AsyncLoggerConfig is stopped. // Stopping the same AsyncLoggerConfig twice results in an incorrect ref count and // the shared Disruptor may be shut down prematurely, resulting in NPE or other errors. if (alreadyStopped.contains(logger)) { continue; } + logger.setNewConfig(newConfiguration.getLoggerConfig(logger.getName())); logger.stop(); loggerCount++; + // clear appenders, even if this logger is already stopped. + logger.clearAppenders(); } LOGGER.trace("AbstractConfiguration stopped {} Loggers.", loggerCount); @@ -275,6 +287,7 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement // Stopping the same AsyncLoggerConfig twice results in an incorrect ref count and // the shared Disruptor may be shut down prematurely, resulting in NPE or other errors. if (!alreadyStopped.contains(root)) { + root.setNewConfig(newConfiguration.getRootLogger()); root.stop(); } super.stop(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3c8fc8c3/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java index 4395069..56eae6c 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configuration.java @@ -17,10 +17,7 @@ package org.apache.logging.log4j.core.config; import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.Appender; -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.*; import org.apache.logging.log4j.core.filter.Filterable; import org.apache.logging.log4j.core.lookup.StrSubstitutor; import org.apache.logging.log4j.core.net.Advertiser; @@ -44,6 +41,18 @@ public interface Configuration extends Filterable { String getName(); /** + * Returns the LoggerContext this configuration is associated with. + * @return The LoggerContext. + */ + LoggerContext getLoggerContext(); + + /** + * Sets the LoggerContext for the Configuration. + * @param loggerContext The LoggerContext this configuration is owned by. + */ + void setLoggerContext(LoggerContext loggerContext); + + /** * Locates the appropriate LoggerConfig for a Logger name. This will remove tokens from the package name as * necessary or return the root LoggerConfig if no other matches were found. * http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/3c8fc8c3/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java index 6109c8f..7bf5a33 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java @@ -81,6 +81,7 @@ public class LoggerConfig extends AbstractFilterable { private final Configuration config; private final Lock shutdownLock = new ReentrantLock(); private final Condition noLogEvents = shutdownLock.newCondition(); // should only be used when shutdown == true + private LoggerConfig newConfig = null; static { final String factory = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_LOG_EVENT_FACTORY); @@ -152,6 +153,12 @@ public class LoggerConfig extends AbstractFilterable { } @Override + public void stop() { + waitForCompletion(); + super.stop(); + } + + @Override public Filter getFilter() { return super.getFilter(); } @@ -183,6 +190,10 @@ public class LoggerConfig extends AbstractFilterable { return this.parent; } + void setNewConfig(LoggerConfig loggerConfig) { + this.newConfig = loggerConfig; + } + /** * Adds an Appender to the LoggerConfig. * @@ -228,7 +239,6 @@ public class LoggerConfig extends AbstractFilterable { * Removes all Appenders. */ protected void clearAppenders() { - waitForCompletion(); List<AppenderControl> copy = new ArrayList<AppenderControl>(appenders); while (!copy.isEmpty()) { appenders.removeAll(copy); @@ -381,7 +391,7 @@ public class LoggerConfig extends AbstractFilterable { try { if (shutdown.compareAndSet(false, true)) { int retries = 0; - while (counter.get() > 0) { + while (counter.compareAndSet(0, Integer.MIN_VALUE)) { try { noLogEvents.await(retries + 1, TimeUnit.SECONDS); } catch (final InterruptedException ie) { @@ -402,18 +412,23 @@ public class LoggerConfig extends AbstractFilterable { * @param event The log event. */ public void log(final LogEvent event) { - beforeLogEvent(); - try { - if (!isFiltered(event)) { - processLogEvent(event); + if (beforeLogEvent()) { + try { + if (!isFiltered(event)) { + processLogEvent(event); + } + } finally { + afterLogEvent(); + } + } else { + if (newConfig != null) { + newConfig.log(event); } - } finally { - afterLogEvent(); } } - private void beforeLogEvent() { - counter.incrementAndGet(); + private boolean beforeLogEvent() { + return counter.incrementAndGet() > 0; } private void afterLogEvent() {
