Skip to content

Commit 99c4e2f

Browse files
committed
The core.Logger#setLevel method should work like
Configurator#setLevel #2281
1 parent e87064f commit 99c4e2f

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

log4j-1.2-api/src/main/java/org/apache/log4j/legacy/core/CategoryUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.logging.log4j.core.Appender;
2828
import org.apache.logging.log4j.core.Filter;
2929
import org.apache.logging.log4j.core.LogEvent;
30+
import org.apache.logging.log4j.core.config.Configurator;
3031
import org.apache.logging.log4j.core.config.LoggerConfig;
3132
import org.apache.logging.log4j.spi.LoggerContext;
3233

@@ -127,7 +128,7 @@ public static void setAdditivity(final Logger logger, final boolean additive) {
127128
*/
128129
public static void setLevel(final Logger logger, final Level level) {
129130
if (isCore(logger)) {
130-
asCore(logger).setLevel(level);
131+
Configurator.setLevel(asCore(logger), level);
131132
}
132133
}
133134

log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@
2626
import java.util.List;
2727
import java.util.Locale;
2828
import java.util.ResourceBundle;
29+
2930
import org.apache.logging.log4j.core.Appender;
3031
import org.apache.logging.log4j.core.LogEvent;
3132
import org.apache.logging.log4j.core.LoggerContext;
3233
import org.apache.logging.log4j.core.appender.AbstractAppender;
3334
import org.apache.logging.log4j.core.config.ConfigurationFactory;
35+
import org.apache.logging.log4j.core.config.Configurator;
3436
import org.apache.logging.log4j.core.config.Property;
3537
import org.apache.logging.log4j.core.layout.PatternLayout;
3638
import org.apache.logging.log4j.core.test.appender.ListAppender;
3739
import org.junit.After;
3840
import org.junit.AfterClass;
41+
import org.junit.Before;
3942
import org.junit.BeforeClass;
4043
import org.junit.Test;
4144

@@ -76,8 +79,10 @@ public static void tearDownClass() {
7679
}
7780

7881
@After
79-
public void tearDown() {
82+
@Before
83+
public void resetTest() {
8084
LoggerContext.getContext().reconfigure();
85+
Configurator.setAllLevels(Logger.getRootLogger().getName(), org.apache.logging.log4j.Level.DEBUG);
8186
a1 = null;
8287
a2 = null;
8388
}
@@ -493,6 +498,48 @@ public void testLog() {
493498
}
494499
}
495500

501+
@Test
502+
public void testSetLevel() {
503+
final Logger a = Logger.getLogger("a");
504+
final Logger a_b = Logger.getLogger("a.b");
505+
final Logger a_b_c = Logger.getLogger("a.b.c");
506+
// test default for this test
507+
assertEquals(Level.DEBUG, a.getLevel());
508+
assertEquals(Level.DEBUG, a_b.getLevel());
509+
assertEquals(Level.DEBUG, a_b_c.getLevel());
510+
// all
511+
for (Level level : new Level[] { /*Level.ALL,*/ Level.DEBUG, Level.ERROR, Level.FATAL, Level.INFO, /*Level.OFF,*/ Level.TRACE, Level.WARN }) {
512+
a.setLevel(level);
513+
assertTrue(level.toString(), a.isEnabledFor(level));
514+
assertTrue(level.toString(), a_b.isEnabledFor(level));
515+
assertTrue(level.toString(), a_b_c.isEnabledFor(level));
516+
assertEquals(level, a.getLevel());
517+
assertEquals(Level.DEBUG, a_b.getLevel());
518+
assertEquals(Level.DEBUG, a_b_c.getLevel());
519+
}
520+
}
521+
522+
@Test
523+
public void testSetPriority() {
524+
final Logger a = Logger.getLogger("a");
525+
final Logger a_b = Logger.getLogger("a.b");
526+
final Logger a_b_c = Logger.getLogger("a.b.c");
527+
// test default for this test
528+
assertEquals(Priority.DEBUG, a.getPriority());
529+
assertEquals(Priority.DEBUG, a_b.getPriority());
530+
assertEquals(Priority.DEBUG, a_b_c.getPriority());
531+
// all
532+
for (Priority level : Level.getAllPossiblePriorities()) {
533+
a.setPriority(level);
534+
assertTrue(level.toString(), a.isEnabledFor(level));
535+
assertTrue(level.toString(), a_b.isEnabledFor(level));
536+
assertTrue(level.toString(), a_b_c.isEnabledFor(level));
537+
assertEquals(level, a.getLevel());
538+
assertEquals(Priority.DEBUG, a_b.getPriority());
539+
assertEquals(Priority.DEBUG, a_b_c.getPriority());
540+
}
541+
}
542+
496543
private static class MyLogger {
497544

498545
private final Logger logger;

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@
542542
<plugin>
543543
<groupId>org.apache.rat</groupId>
544544
<artifactId>apache-rat-plugin</artifactId>
545+
<version>0.16.1</version>
545546
<configuration>
546547
<consoleOutput>true</consoleOutput>
547548
<excludes combine.children="append">
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+
The core.Logger#setLevel method should work like Configurator#setLevel.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)