This is an automated email from the ASF dual-hosted git repository. mattsicker pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 17485c10e646e834313953e0c308c0cac932e665 Author: Matt Sicker <[email protected]> AuthorDate: Sun Oct 30 18:31:01 2022 -0500 Add more downcast checks in LoggerContext This provides a slightly more useful error in contexts where the LoggerContextFactory doesn't seem to be set up right. Signed-off-by: Matt Sicker <[email protected]> --- .../java/org/apache/logging/log4j/core/LoggerContext.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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 ef93e4d08c..af67daffbc 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 @@ -202,6 +202,13 @@ public class LoggerContext extends AbstractLifeCycle return listeners; } + private static LoggerContext downcast(final org.apache.logging.log4j.spi.LoggerContext context) { + if (context instanceof LoggerContext) { + return (LoggerContext) context; + } + throw new IllegalStateException("Expected org.apache.logging.log4j.core.LoggerContext but got " + context.getClass()); + } + /** * Returns the current LoggerContext. * <p> @@ -221,7 +228,7 @@ public class LoggerContext extends AbstractLifeCycle * @see LogManager#getContext() */ public static LoggerContext getContext() { - return (LoggerContext) LogManager.getContext(); + return downcast(LogManager.getContext()); } /** @@ -242,7 +249,7 @@ public class LoggerContext extends AbstractLifeCycle * @see LogManager#getContext(boolean) */ public static LoggerContext getContext(final boolean currentContext) { - return (LoggerContext) LogManager.getContext(currentContext); + return downcast(LogManager.getContext(currentContext)); } /** @@ -267,7 +274,7 @@ public class LoggerContext extends AbstractLifeCycle */ public static LoggerContext getContext(final ClassLoader loader, final boolean currentContext, final URI configLocation) { - return (LoggerContext) LogManager.getContext(loader, currentContext, configLocation); + return downcast(LogManager.getContext(loader, currentContext, configLocation)); } @Override
