From 9e71d44481f199fed2eb5a850c8124cdfe0e6d7f Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 15 Feb 2024 16:08:03 -0500 Subject: [PATCH] [Issue2281] The core.Logger#setLevel method should work like Configurator#setLevel() (#2289) * [Issue2281] The core.Logger#setLevel method should work like Configurator#setLevel #2281 Do for this module what we did for log4j-1.2-api * Update log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CoreLoggerTest.java Co-authored-by: Piotr P. Karwasz --------- Co-authored-by: Piotr P. Karwasz --- .../apache/logging/log4j/jul/CoreLogger.java | 3 +- .../log4j/jul/test/CoreLoggerTest.java | 35 +++++++++++++++++++ .../.2.x.x/2282_fix_jul_set_level.xml | 10 ++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/changelog/.2.x.x/2282_fix_jul_set_level.xml diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/CoreLogger.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/CoreLogger.java index ebc06985a8b..02c2007d35b 100644 --- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/CoreLogger.java +++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/CoreLogger.java @@ -18,6 +18,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.logging.log4j.core.config.Configurator; /** * Log4j Core implementation of the JUL {@link Logger} class. Note that this implementation does @@ -44,7 +45,7 @@ public class CoreLogger extends ApiLogger { @Override public void setLevel(final Level level) throws SecurityException { super.doSetLevel(level); // checks permissions - logger.setLevel(LevelTranslator.toLevel(level)); + Configurator.setLevel(logger, LevelTranslator.toLevel(level)); } /** diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CoreLoggerTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CoreLoggerTest.java index 57d63b9372c..d554758a4fa 100644 --- a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CoreLoggerTest.java +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CoreLoggerTest.java @@ -18,8 +18,10 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import java.util.logging.Level; import java.util.logging.Logger; @@ -46,6 +48,7 @@ public static void tearDownClass() { @Before public void setUp() throws Exception { + LogManager.getLogManager().reset(); logger = Logger.getLogger(LOGGER_NAME); logger.setFilter(null); assertThat(logger.getLevel(), equalTo(Level.FINE)); @@ -100,6 +103,38 @@ public void testSetLevel() throws Exception { assertThat(childLogger.isLoggable(Level.ALL), is(false)); } + @Test + public void testSetLevelIssue2281() { + 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.INFO, a.getLevel()); + assertEquals(Level.INFO, a_b.getLevel()); + assertEquals(Level.INFO, a_b_c.getLevel()); + // all levels + final Level[] levels = new Level[] { + Level.OFF, + Level.SEVERE, + Level.WARNING, + Level.INFO, + Level.CONFIG, + Level.FINE, + Level.FINER, + Level.FINEST, + Level.ALL + }; + for (int i = 0; i < levels.length - 1; i++) { + final Level level = levels[i]; + final Level nextLevel = levels[i + 1]; + a.setLevel(level); + assertEquals(level, a.getLevel()); + assertTrue(a.isLoggable(level) && !a.isLoggable(nextLevel)); + assertTrue(a_b.isLoggable(level) && !a.isLoggable(nextLevel)); + assertTrue(a_b_c.isLoggable(level) && !a.isLoggable(nextLevel)); + } + } + @Test public void testSetLevelToNull() throws Exception { final Logger childLogger = Logger.getLogger(LOGGER_NAME + ".NullChild"); diff --git a/src/changelog/.2.x.x/2282_fix_jul_set_level.xml b/src/changelog/.2.x.x/2282_fix_jul_set_level.xml new file mode 100644 index 00000000000..66f6e1e51ed --- /dev/null +++ b/src/changelog/.2.x.x/2282_fix_jul_set_level.xml @@ -0,0 +1,10 @@ + + + + + Fix the behavior of `CoreLogger#setLevel` in the log4j-jul module. + +