Skip to content

Commit 9e71d44

Browse files
[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 <[email protected]> --------- Co-authored-by: Piotr P. Karwasz <[email protected]>
1 parent 386819d commit 9e71d44

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

log4j-jul/src/main/java/org/apache/logging/log4j/jul/CoreLogger.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.logging.Level;
2020
import java.util.logging.Logger;
21+
import org.apache.logging.log4j.core.config.Configurator;
2122

2223
/**
2324
* Log4j Core implementation of the JUL {@link Logger} class. <strong>Note that this implementation does
@@ -44,7 +45,7 @@ public class CoreLogger extends ApiLogger {
4445
@Override
4546
public void setLevel(final Level level) throws SecurityException {
4647
super.doSetLevel(level); // checks permissions
47-
logger.setLevel(LevelTranslator.toLevel(level));
48+
Configurator.setLevel(logger, LevelTranslator.toLevel(level));
4849
}
4950

5051
/**

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CoreLoggerTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import static org.hamcrest.Matchers.equalTo;
2020
import static org.hamcrest.Matchers.is;
21+
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertThat;
24+
import static org.junit.Assert.assertTrue;
2325

2426
import java.util.logging.Level;
2527
import java.util.logging.Logger;
@@ -46,6 +48,7 @@ public static void tearDownClass() {
4648

4749
@Before
4850
public void setUp() throws Exception {
51+
LogManager.getLogManager().reset();
4952
logger = Logger.getLogger(LOGGER_NAME);
5053
logger.setFilter(null);
5154
assertThat(logger.getLevel(), equalTo(Level.FINE));
@@ -100,6 +103,38 @@ public void testSetLevel() throws Exception {
100103
assertThat(childLogger.isLoggable(Level.ALL), is(false));
101104
}
102105

106+
@Test
107+
public void testSetLevelIssue2281() {
108+
final Logger a = Logger.getLogger("a");
109+
final Logger a_b = Logger.getLogger("a.b");
110+
final Logger a_b_c = Logger.getLogger("a.b.c");
111+
// test default for this test
112+
assertEquals(Level.INFO, a.getLevel());
113+
assertEquals(Level.INFO, a_b.getLevel());
114+
assertEquals(Level.INFO, a_b_c.getLevel());
115+
// all levels
116+
final Level[] levels = new Level[] {
117+
Level.OFF,
118+
Level.SEVERE,
119+
Level.WARNING,
120+
Level.INFO,
121+
Level.CONFIG,
122+
Level.FINE,
123+
Level.FINER,
124+
Level.FINEST,
125+
Level.ALL
126+
};
127+
for (int i = 0; i < levels.length - 1; i++) {
128+
final Level level = levels[i];
129+
final Level nextLevel = levels[i + 1];
130+
a.setLevel(level);
131+
assertEquals(level, a.getLevel());
132+
assertTrue(a.isLoggable(level) && !a.isLoggable(nextLevel));
133+
assertTrue(a_b.isLoggable(level) && !a.isLoggable(nextLevel));
134+
assertTrue(a_b_c.isLoggable(level) && !a.isLoggable(nextLevel));
135+
}
136+
}
137+
103138
@Test
104139
public void testSetLevelToNull() throws Exception {
105140
final Logger childLogger = Logger.getLogger(LOGGER_NAME + ".NullChild");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://logging.apache.org/log4j/changelog"
4+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
5+
type="fixed">
6+
<issue id="2282" link="https://github.com/apache/logging-log4j2/issues/2282"/>
7+
<description format="asciidoc">
8+
Fix the behavior of `CoreLogger#setLevel` in the log4j-jul module.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)