You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2022/01/12 14:00:42 UTC

[logging-log4j2] branch master updated: [LOG4J2-3330] Configurator.setLevel not fetching the correct LoggerContext.

This is an automated email from the ASF dual-hosted git repository.

ggregory 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 606bea1  [LOG4J2-3330] Configurator.setLevel not fetching the correct LoggerContext.
606bea1 is described below

commit 606bea12b2fa1399e7756aefdc1e422ab1a7c28c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jan 12 08:09:04 2022 -0500

    [LOG4J2-3330] Configurator.setLevel not fetching the correct
    LoggerContext.
---
 .../logging/log4j/core/config/Configurator.java     | 21 ++++++++++++---------
 src/changes/changes.xml                             |  3 +++
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
index 9e225a9..a27b944 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/Configurator.java
@@ -30,6 +30,7 @@ import org.apache.logging.log4j.core.impl.Log4jContextFactory;
 import org.apache.logging.log4j.core.util.NetUtils;
 import org.apache.logging.log4j.spi.LoggerContextFactory;
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.StackLocatorUtil;
 import org.apache.logging.log4j.util.Strings;
 
 /**
@@ -303,6 +304,7 @@ public final class Configurator {
 
     /**
      * Sets the levels of <code>parentLogger</code> and all 'child' loggers to the given <code>level</code>.
+     *
      * @param parentLogger the parent logger
      * @param level the new level
      */
@@ -312,7 +314,7 @@ public final class Configurator {
         // 3) set level on logger config
         // 4) update child logger configs with level
         // 5) update loggers
-        final LoggerContext loggerContext = LoggerContext.getContext(false);
+        final LoggerContext loggerContext = LoggerContext.getContext(StackLocatorUtil.getCallerClassLoader(2), false, null);
         final Configuration config = loggerContext.getConfiguration();
         boolean set = setLevel(parentLogger, level, config);
         for (final Map.Entry<String, LoggerConfig> entry : config.getLoggers().entrySet()) {
@@ -341,7 +343,7 @@ public final class Configurator {
      *            Levels.
      */
     public static void setLevel(final Map<String, Level> levelMap) {
-        final LoggerContext loggerContext = LoggerContext.getContext(false);
+        final LoggerContext loggerContext = LoggerContext.getContext(StackLocatorUtil.getCallerClassLoader(2), false, null);
         final Configuration config = loggerContext.getConfiguration();
         boolean set = false;
         for (final Map.Entry<String, Level> entry : levelMap.entrySet()) {
@@ -363,13 +365,11 @@ public final class Configurator {
      *            the new level
      */
     public static void setLevel(final String loggerName, final Level level) {
-        final LoggerContext loggerContext = LoggerContext.getContext(false);
+        final LoggerContext loggerContext = LoggerContext.getContext(StackLocatorUtil.getCallerClassLoader(2), false, null);
         if (Strings.isEmpty(loggerName)) {
-            setRootLevel(level);
-        } else {
-            if (setLevel(loggerName, level, loggerContext.getConfiguration())) {
-                loggerContext.updateLoggers();
-            }
+            setRootLevel(level, loggerContext);
+        } else if (setLevel(loggerName, level, loggerContext.getConfiguration())) {
+            loggerContext.updateLoggers();
         }
     }
 
@@ -395,7 +395,10 @@ public final class Configurator {
      *            the new level
      */
     public static void setRootLevel(final Level level) {
-        final LoggerContext loggerContext = LoggerContext.getContext(false);
+        setRootLevel(level, LoggerContext.getContext(StackLocatorUtil.getCallerClassLoader(2), false, null));
+    }
+
+    private static void setRootLevel(final Level level, final LoggerContext loggerContext) {
         final LoggerConfig loggerConfig = loggerContext.getConfiguration().getRootLogger();
         if (!loggerConfig.getLevel().equals(level)) {
             loggerConfig.setLevel(level);
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 06f14d7..23994ed 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -266,6 +266,9 @@
       <action dev="ggregory" type="fix" issue="LOG4J2-3326" due-to="Gary Gregory">
         Log4j 1.2 bridge class org.apache.log4j.spi.LoggingEvent missing constructors and public instance variable.
       </action>
+      <action dev="ggregory" type="fix" issue="LOG4J2-3330" due-to="Mircea Lemnaru, Gary Gregory">
+        Configurator.setLevel not fetching the correct LoggerContext.
+      </action>
       <!-- ADD -->
       <action issue="LOG4J2-3282" dev="ckozak" type="add" due-to="Michael Vorburger">
         Add the log4j-to-jul JDK Logging Bridge