Skip to content

Commit

Permalink
The core.Logger#setLevel method should work like
Browse files Browse the repository at this point in the history
Configurator#setLevel #2281
  • Loading branch information
garydgregory committed Feb 13, 2024
1 parent e87064f commit 5bf9bbd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.spi.LoggerContext;

Expand Down Expand Up @@ -127,7 +128,7 @@ public static void setAdditivity(final Logger logger, final boolean additive) {
*/
public static void setLevel(final Logger logger, final Level level) {
if (isCore(logger)) {
asCore(logger).setLevel(level);
Configurator.setLevel(asCore(logger), level);
}
}

Expand Down
43 changes: 42 additions & 1 deletion log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -76,8 +79,10 @@ public static void tearDownClass() {
}

@After
public void tearDown() {
@Before
public void resetTest() {
LoggerContext.getContext().reconfigure();
Configurator.setAllLevels(Logger.getRootLogger().getName(), org.apache.logging.log4j.Level.DEBUG);
a1 = null;
a2 = null;
}
Expand Down Expand Up @@ -493,6 +498,42 @@ public void testLog() {
}
}

@Test
public void testSetLevel() {
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.DEBUG, a.getLevel());
assertEquals(Level.DEBUG, a_b.getLevel());
assertEquals(Level.DEBUG, a_b_c.getLevel());
// all
for (Level level : new Level[] { Level.ALL, Level.DEBUG, Level.ERROR, Level.FATAL, Level.INFO, Level.OFF, Level.TRACE, Level.WARN }) {
a.setLevel(level);
assertEquals(level, a.getLevel());
assertEquals(level, a_b.getLevel());
assertEquals(level, a_b_c.getLevel());
}
}

@Test
public void testSetPriority() {
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(Priority.DEBUG, a.getPriority());
assertEquals(Priority.DEBUG, a_b.getPriority());
assertEquals(Priority.DEBUG, a_b_c.getPriority());
// all
for (Priority level : Level.getAllPossiblePriorities()) {
a.setPriority(level);
assertEquals(level, a.getLevel());
assertEquals(level, a_b.getLevel());
assertEquals(level, a_b_c.getLevel());
}
}

private static class MyLogger {

private final Logger logger;
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@
<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<version>0.16.1</version>
<configuration>
<consoleOutput>true</consoleOutput>
<excludes combine.children="append">
Expand Down
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/2282_fix_1_2_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">
The core.Logger#setLevel method should work like Configurator#setLevel.
</description>
</entry>

0 comments on commit 5bf9bbd

Please sign in to comment.