From aacad7c1e97bd3bd1720b60387f6b76fad2f20c0 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 15 Feb 2024 09:04:24 -0500 Subject: [PATCH] [Issue2281] The core.Logger#setLevel method should work like Configurator#setLevel #2281 Do for this module what we did for log4j-1.2-api --- .../apache/logging/log4j/jul/CoreLogger.java | 4 ++- .../log4j/jul/test/CoreLoggerTest.java | 32 +++++++++++++++++++ .../.2.x.x/2282_fix_jul_set_level.xml | 10 ++++++ 3 files changed, 45 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..1fddca524b2 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 @@ -19,6 +19,8 @@ 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 * not use the {@link java.util.logging.Handler} class. Instead, logging is delegated to the @@ -44,7 +46,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..2cd4a2c918d 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 @@ -20,6 +20,8 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +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,35 @@ 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 + for (final Level level : new Level[] { + Level.ALL, + Level.CONFIG, + Level.FINE, + Level.FINER, + Level.FINEST, + Level.INFO, + Level.OFF, + Level.SEVERE, + Level.WARNING + }) { + a.setLevel(level); + assertEquals(level, a.getLevel()); + assertTrue(a.isLoggable(level)); + assertTrue(a_b.isLoggable(level)); + assertTrue(a_b_c.isLoggable(level)); + } + } + @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. + +