Skip to content

Commit

Permalink
[Issue2281] The core.Logger#setLevel method should work like
Browse files Browse the repository at this point in the history
Configurator#setLevel apache#2281

Do for this module what we did for log4j-1.2-api
  • Loading branch information
garydgregory committed Feb 15, 2024
1 parent 386819d commit aacad7c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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. <strong>Note that this implementation does
* <em>not</em> use the {@link java.util.logging.Handler} class.</strong> Instead, logging is delegated to the
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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,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");
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 aacad7c

Please sign in to comment.