From 5bf9bbda725aab9a937393907fd896d536fad73d Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 12 Feb 2024 23:06:43 -0500 Subject: [PATCH] The core.Logger#setLevel method should work like Configurator#setLevel #2281 --- .../log4j/legacy/core/CategoryUtil.java | 3 +- .../java/org/apache/log4j/LoggerTest.java | 43 ++++++++++++++++++- pom.xml | 1 + .../.2.x.x/2282_fix_1_2_set_level.xml | 10 +++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/changelog/.2.x.x/2282_fix_1_2_set_level.xml diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/legacy/core/CategoryUtil.java b/log4j-1.2-api/src/main/java/org/apache/log4j/legacy/core/CategoryUtil.java index ee31ff53802..b51b1b466fc 100644 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/legacy/core/CategoryUtil.java +++ b/log4j-1.2-api/src/main/java/org/apache/log4j/legacy/core/CategoryUtil.java @@ -27,6 +27,7 @@ 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.config.Configurator; import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.spi.LoggerContext; @@ -127,7 +128,7 @@ public static void setAdditivity(final Logger logger, final boolean additive) { */ public static void setLevel(final Logger logger, final Level level) { if (isCore(logger)) { - asCore(logger).setLevel(level); + Configurator.setLevel(asCore(logger), level); } } diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java index 55d54321119..93df45f7f1c 100644 --- a/log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java @@ -26,16 +26,19 @@ import java.util.List; import java.util.Locale; import java.util.ResourceBundle; + import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.config.Property; import org.apache.logging.log4j.core.layout.PatternLayout; import org.apache.logging.log4j.core.test.appender.ListAppender; import org.junit.After; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -76,8 +79,10 @@ public static void tearDownClass() { } @After - public void tearDown() { + @Before + public void resetTest() { LoggerContext.getContext().reconfigure(); + Configurator.setAllLevels(Logger.getRootLogger().getName(), org.apache.logging.log4j.Level.DEBUG); a1 = null; a2 = null; } @@ -493,6 +498,42 @@ public void testLog() { } } + @Test + public void testSetLevel() { + final Logger a = Logger.getLogger("a"); + final Logger a_b = Logger.getLogger("a.b"); + final Logger a_b_c = Logger.getLogger("a.b.c"); + // test default for this test + assertEquals(Level.DEBUG, a.getLevel()); + assertEquals(Level.DEBUG, a_b.getLevel()); + assertEquals(Level.DEBUG, a_b_c.getLevel()); + // all + for (Level level : new Level[] { Level.ALL, Level.DEBUG, Level.ERROR, Level.FATAL, Level.INFO, Level.OFF, Level.TRACE, Level.WARN }) { + a.setLevel(level); + assertEquals(level, a.getLevel()); + assertEquals(level, a_b.getLevel()); + assertEquals(level, a_b_c.getLevel()); + } + } + + @Test + public void testSetPriority() { + final Logger a = Logger.getLogger("a"); + final Logger a_b = Logger.getLogger("a.b"); + final Logger a_b_c = Logger.getLogger("a.b.c"); + // test default for this test + assertEquals(Priority.DEBUG, a.getPriority()); + assertEquals(Priority.DEBUG, a_b.getPriority()); + assertEquals(Priority.DEBUG, a_b_c.getPriority()); + // all + for (Priority level : Level.getAllPossiblePriorities()) { + a.setPriority(level); + assertEquals(level, a.getLevel()); + assertEquals(level, a_b.getLevel()); + assertEquals(level, a_b_c.getLevel()); + } + } + private static class MyLogger { private final Logger logger; diff --git a/pom.xml b/pom.xml index 035fc1d2368..18e2be83d5c 100644 --- a/pom.xml +++ b/pom.xml @@ -542,6 +542,7 @@ org.apache.rat apache-rat-plugin + 0.16.1 true diff --git a/src/changelog/.2.x.x/2282_fix_1_2_set_level.xml b/src/changelog/.2.x.x/2282_fix_1_2_set_level.xml new file mode 100644 index 00000000000..30b7a9381df --- /dev/null +++ b/src/changelog/.2.x.x/2282_fix_1_2_set_level.xml @@ -0,0 +1,10 @@ + + + + + The core.Logger#setLevel method should work like Configurator#setLevel. + +