Repository: logging-log4j2 Updated Branches: refs/heads/master 924bcafab -> 7268c2d44
LOG4J2-1222 - Prevent error message and use SimpleLogger after shutdown Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/7268c2d4 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/7268c2d4 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/7268c2d4 Branch: refs/heads/master Commit: 7268c2d4464c5fb058d0fd520d11944105350193 Parents: 924bcaf Author: Ralph Goers <[email protected]> Authored: Sun Feb 7 20:53:32 2016 -0700 Committer: Ralph Goers <[email protected]> Committed: Sun Feb 7 20:53:32 2016 -0700 ---------------------------------------------------------------------- .../org/apache/logging/log4j/LogManager.java | 66 +++++++++++++++++--- .../logging/log4j/core/LoggerContext.java | 4 +- 2 files changed, 59 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7268c2d4/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java index 8b7f389..8c4260b 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java @@ -151,7 +151,12 @@ public class LogManager { * @return The current LoggerContext. */ public static LoggerContext getContext() { - return factory.getContext(FQCN, null, null, true); + try { + return factory.getContext(FQCN, null, null, true); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(FQCN, null, null, true); + } } /** @@ -165,7 +170,12 @@ public class LogManager { */ public static LoggerContext getContext(final boolean currentContext) { // TODO: would it be a terrible idea to try and find the caller ClassLoader here? - return factory.getContext(FQCN, null, null, currentContext, null, null); + try { + return factory.getContext(FQCN, null, null, currentContext, null, null); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(FQCN, null, null, currentContext, null, null); + } } /** @@ -180,7 +190,12 @@ public class LogManager { * @return a LoggerContext. */ public static LoggerContext getContext(final ClassLoader loader, final boolean currentContext) { - return factory.getContext(FQCN, loader, null, currentContext); + try { + return factory.getContext(FQCN, loader, null, currentContext); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(FQCN, loader, null, currentContext); + } } /** @@ -197,7 +212,12 @@ public class LogManager { */ public static LoggerContext getContext(final ClassLoader loader, final boolean currentContext, final Object externalContext) { - return factory.getContext(FQCN, loader, externalContext, currentContext); + try { + return factory.getContext(FQCN, loader, externalContext, currentContext); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(FQCN, loader, externalContext, currentContext); + } } /** @@ -214,7 +234,13 @@ public class LogManager { */ public static LoggerContext getContext(final ClassLoader loader, final boolean currentContext, final URI configLocation) { - return factory.getContext(FQCN, loader, null, currentContext, configLocation, null); + try { + return factory.getContext(FQCN, loader, null, currentContext, configLocation, null); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(FQCN, loader, null, currentContext, configLocation, + null); + } } /** @@ -232,7 +258,13 @@ public class LogManager { */ public static LoggerContext getContext(final ClassLoader loader, final boolean currentContext, final Object externalContext, final URI configLocation) { - return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, null); + try { + return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, null); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(FQCN, loader, externalContext, currentContext, + configLocation, null); + } } /** @@ -251,7 +283,13 @@ public class LogManager { */ public static LoggerContext getContext(final ClassLoader loader, final boolean currentContext, final Object externalContext, final URI configLocation, final String name) { - return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, name); + try { + return factory.getContext(FQCN, loader, externalContext, currentContext, configLocation, name); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(FQCN, loader, externalContext, currentContext, + configLocation, name); + } } /** @@ -265,7 +303,12 @@ public class LogManager { * @return a LoggerContext. */ protected static LoggerContext getContext(final String fqcn, final boolean currentContext) { - return factory.getContext(fqcn, null, null, currentContext); + try { + return factory.getContext(fqcn, null, null, currentContext); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(fqcn, null, null, currentContext); + } } /** @@ -282,7 +325,12 @@ public class LogManager { */ protected static LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) { - return factory.getContext(fqcn, loader, null, currentContext); + try { + return factory.getContext(fqcn, loader, null, currentContext); + } catch (IllegalStateException ex) { + LOGGER.warn(ex.getMessage() + " Using SimpleLogger"); + return new SimpleLoggerContextFactory().getContext(fqcn, loader, null, currentContext); + } } /** http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7268c2d4/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 8e19936..4b80054 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 @@ -268,8 +268,8 @@ public class LoggerContext extends AbstractLifeCycle implements org.apache.loggi } }); } catch (final IllegalStateException e) { - LOGGER.error(SHUTDOWN_HOOK_MARKER, - "Unable to register shutdown hook because JVM is shutting down.", e); + throw new IllegalStateException( + "Unable to register Log4j shutdown hook because JVM is shutting down.", e); } catch (final SecurityException e) { LOGGER.error(SHUTDOWN_HOOK_MARKER, "Unable to register shutdown hook due to security restrictions", e);
