Skip to content

Commit 0ecf6e1

Browse files
The core.Logger#setLevel method should work like (#2282)
* The core.Logger#setLevel method should work like Configurator#setLevel #2281 * Fix `Logger#setLevel` to work as in Log4j 1.2 --------- Co-authored-by: Piotr P. Karwasz <[email protected]>
1 parent 6a3f8cd commit 0ecf6e1

File tree

7 files changed

+115
-39
lines changed

7 files changed

+115
-39
lines changed

log4j-1.2-api/src/main/java/org/apache/log4j/Category.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.log4j.bridge.LogEventWrapper;
3030
import org.apache.log4j.helpers.AppenderAttachableImpl;
3131
import org.apache.log4j.helpers.NullEnumeration;
32+
import org.apache.log4j.helpers.OptionConverter;
3233
import org.apache.log4j.legacy.core.CategoryUtil;
3334
import org.apache.log4j.spi.AppenderAttachable;
3435
import org.apache.log4j.spi.HierarchyEventListener;
@@ -384,25 +385,7 @@ public Priority getChainedPriority() {
384385
}
385386

386387
public Level getEffectiveLevel() {
387-
switch (logger.getLevel().getStandardLevel()) {
388-
case ALL:
389-
return Level.ALL;
390-
case TRACE:
391-
return Level.TRACE;
392-
case DEBUG:
393-
return Level.DEBUG;
394-
case INFO:
395-
return Level.INFO;
396-
case WARN:
397-
return Level.WARN;
398-
case ERROR:
399-
return Level.ERROR;
400-
case FATAL:
401-
return Level.FATAL;
402-
default:
403-
// TODO Should this be an IllegalStateException?
404-
return Level.OFF;
405-
}
388+
return OptionConverter.convertLevel(logger.getLevel());
406389
}
407390

408391
/**
@@ -417,7 +400,8 @@ public LoggerRepository getHierarchy() {
417400
}
418401

419402
public final Level getLevel() {
420-
return getEffectiveLevel();
403+
final org.apache.logging.log4j.Level v2Level = CategoryUtil.getExplicitLevel(logger);
404+
return v2Level != null ? OptionConverter.convertLevel(v2Level) : null;
421405
}
422406

423407
private String getLevelStr(final Priority priority) {
@@ -460,7 +444,7 @@ public final Category getParent() {
460444
}
461445

462446
public final Level getPriority() {
463-
return getEffectiveLevel();
447+
return getLevel();
464448
}
465449

466450
public ResourceBundle getResourceBundle() {

log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,16 +630,36 @@ public static Level toLevel(final String clazz, final String levelName, final Le
630630
return null;
631631
}
632632

633-
LOGGER.debug("toLevel" + ":class=[" + clazz + "]" + ":pri=[" + levelName + "]");
633+
LOGGER.debug("toLevel:class=[{}]:pri=[{}]", clazz, levelName);
634634

635635
// Support for levels defined in Log4j2.
636636
if (LOG4J2_LEVEL_CLASS.equals(clazz)) {
637637
final org.apache.logging.log4j.Level v2Level =
638638
org.apache.logging.log4j.Level.getLevel(toRootUpperCase(levelName));
639639
if (v2Level != null) {
640-
return new LevelWrapper(v2Level);
640+
switch (v2Level.name()) {
641+
case "ALL":
642+
return Level.ALL;
643+
case "DEBUG":
644+
return Level.DEBUG;
645+
case "ERROR":
646+
return Level.ERROR;
647+
case "FATAL":
648+
return Level.FATAL;
649+
case "INFO":
650+
return Level.INFO;
651+
case "OFF":
652+
return Level.OFF;
653+
case "WARN":
654+
return Level.WARN;
655+
case "TRACE":
656+
return Level.TRACE;
657+
default:
658+
return new LevelWrapper(v2Level);
659+
}
660+
} else {
661+
return defaultValue;
641662
}
642-
return defaultValue;
643663
}
644664
try {
645665
final Class<?> customLevel = LoaderUtil.loadClass(clazz);

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

Lines changed: 17 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,10 +128,25 @@ 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

135+
/**
136+
* Returns the level explicitly set on the logger.
137+
* <p>
138+
* If the Log4j API implementation does not support it, returns the effective level instead.
139+
* </p>
140+
*/
141+
public static Level getExplicitLevel(final Logger logger) {
142+
return isCore(logger) ? getExplicitLevel(asCore(logger)) : logger.getLevel();
143+
}
144+
145+
private static Level getExplicitLevel(final org.apache.logging.log4j.core.Logger logger) {
146+
final LoggerConfig config = logger.get();
147+
return config.getName().equals(logger.getName()) ? config.getExplicitLevel() : null;
148+
}
149+
134150
/**
135151
* Adds an appender to the logger. This method requires a check for the presence
136152
* of Log4j Core or it will cause a {@code ClassNotFoundException}.

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.log4j;
1818

19+
import static org.assertj.core.api.Assertions.assertThat;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertNotNull;
@@ -25,17 +26,17 @@
2526

2627
import java.util.List;
2728
import java.util.Locale;
29+
import java.util.Objects;
2830
import java.util.ResourceBundle;
2931
import org.apache.logging.log4j.core.Appender;
3032
import org.apache.logging.log4j.core.LogEvent;
31-
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;
3435
import org.apache.logging.log4j.core.config.Property;
3536
import org.apache.logging.log4j.core.layout.PatternLayout;
3637
import org.apache.logging.log4j.core.test.appender.ListAppender;
37-
import org.junit.After;
3838
import org.junit.AfterClass;
39+
import org.junit.Before;
3940
import org.junit.BeforeClass;
4041
import org.junit.Test;
4142

@@ -75,9 +76,9 @@ public static void tearDownClass() {
7576
ConfigurationFactory.removeConfigurationFactory(configurationFactory);
7677
}
7778

78-
@After
79-
public void tearDown() {
80-
LoggerContext.getContext().reconfigure();
79+
@Before
80+
public void resetTest() {
81+
Objects.requireNonNull(LogManager.getHierarchy()).resetConfiguration();
8182
a1 = null;
8283
a2 = null;
8384
}
@@ -493,6 +494,53 @@ public void testLog() {
493494
}
494495
}
495496

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

498546
private final Logger logger;

log4j-1.2-api/src/test/java/org/apache/log4j/helpers/OptionConverterLevelTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
import static org.apache.log4j.helpers.OptionConverter.toLog4j1Level;
2020
import static org.apache.log4j.helpers.OptionConverter.toLog4j2Level;
21-
import static org.junit.Assert.assertNull;
21+
import static org.assertj.core.api.Assertions.assertThat;
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.junit.jupiter.api.Assertions.assertNull;
2425

2526
import java.util.Arrays;
2627
import java.util.stream.Stream;
@@ -43,22 +44,18 @@ static Stream<Arguments> standardLevels() {
4344

4445
/**
4546
* Test if the standard levels are transformed correctly.
46-
*
47-
* @param log4j1Level
48-
* @param log4j2Level
4947
*/
5048
@ParameterizedTest
5149
@MethodSource("standardLevels")
5250
public void testStandardLevelConversion(final Level log4j1Level, final org.apache.logging.log4j.Level log4j2Level) {
53-
assertEquals(log4j2Level, OptionConverter.convertLevel(log4j1Level));
54-
assertEquals(log4j1Level, OptionConverter.convertLevel(log4j2Level));
51+
assertThat(log4j2Level).isSameAs(OptionConverter.convertLevel(log4j1Level));
52+
assertThat(log4j1Level).isSameAs(OptionConverter.convertLevel(log4j2Level));
53+
assertThat(OptionConverter.toLevel(org.apache.logging.log4j.Level.class.getName(), log4j2Level.name(), null))
54+
.isSameAs(OptionConverter.convertLevel(log4j2Level));
5555
}
5656

5757
/**
5858
* Test if the conversion works at an integer level.
59-
*
60-
* @param log4j1Level
61-
* @param log4j2Level
6259
*/
6360
@ParameterizedTest
6461
@MethodSource("standardLevels")

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+
Fix the behavior of `Logger#setLevel` and `Logger#getLevel` in the Log4j 1.2 bridge.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)