Skip to content

Commit

Permalink
[Issue2281] The core.Logger#setLevel method should work like Configur…
Browse files Browse the repository at this point in the history
…ator#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]>
  • Loading branch information
garydgregory and ppkarwasz authored Feb 15, 2024
1 parent 386819d commit 9e71d44
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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. <strong>Note that this implementation does
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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");
Expand Down
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/2282_fix_jul_set_level.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
type="fixed">
<issue id="2282" link="https://github.com/apache/logging-log4j2/issues/2282"/>
<description format="asciidoc">
Fix the behavior of `CoreLogger#setLevel` in the log4j-jul module.
</description>
</entry>

0 comments on commit 9e71d44

Please sign in to comment.