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 13:09:14 UTC
[logging-log4j2] 02/03: [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 release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 585df0052ccf7a9d0ff2bc2a625c975ff9eceb09
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jan 12 08:09:04 2022 -0500
[LOG4J2-3330] Configurator.setLevel not fetching the correct
LoggerContext.
---
.../apache/logging/log4j/core/config/Configurator.java | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 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 6ed1420..de69bcf 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
@@ -24,6 +24,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;
import java.net.URI;
@@ -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,9 +365,9 @@ 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);
+ setRootLevel(level, loggerContext);
} else if (setLevel(loggerName, level, loggerContext.getConfiguration())) {
loggerContext.updateLoggers();
}
@@ -393,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);