Skip to content

Commit dcf3076

Browse files
committed
Add guards to mutator methods when compatibility mode is off
1 parent 8ec5703 commit dcf3076

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
import org.apache.log4j.spi.HierarchyEventListener;
3636
import org.apache.log4j.spi.LoggerRepository;
3737
import org.apache.log4j.spi.LoggingEvent;
38+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
3839
import org.apache.logging.log4j.message.LocalizedMessage;
3940
import org.apache.logging.log4j.message.MapMessage;
4041
import org.apache.logging.log4j.message.Message;
4142
import org.apache.logging.log4j.message.ObjectMessage;
4243
import org.apache.logging.log4j.message.SimpleMessage;
4344
import org.apache.logging.log4j.spi.ExtendedLogger;
4445
import org.apache.logging.log4j.spi.LoggerContext;
46+
import org.apache.logging.log4j.util.PropertiesUtil;
4547
import org.apache.logging.log4j.util.StackLocatorUtil;
4648
import org.apache.logging.log4j.util.Strings;
4749

@@ -52,6 +54,10 @@ public class Category implements AppenderAttachable {
5254

5355
private static final String FQCN = Category.class.getName();
5456

57+
private static boolean isFullCompatibilityEnabled() {
58+
return PropertiesUtil.getProperties().getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL);
59+
}
60+
5561
/**
5662
* Tests if the named category exists (in the default hierarchy).
5763
*
@@ -192,6 +198,9 @@ protected Category(final String name) {
192198
*/
193199
@Override
194200
public void addAppender(final Appender appender) {
201+
if (!isFullCompatibilityEnabled()) {
202+
return;
203+
}
195204
if (appender != null) {
196205
if (LogManager.isLog4jCorePresent()) {
197206
CategoryUtil.addAppender(logger, AppenderAdapter.adapt(appender));
@@ -572,6 +581,9 @@ void maybeLog(
572581
*/
573582
@Override
574583
public void removeAllAppenders() {
584+
if (!isFullCompatibilityEnabled()) {
585+
return;
586+
}
575587
if (aai != null) {
576588
final Vector appenders = new Vector();
577589
for (final Enumeration iter = aai.getAllAppenders(); iter != null && iter.hasMoreElements(); ) {
@@ -593,6 +605,9 @@ public void removeAllAppenders() {
593605
*/
594606
@Override
595607
public void removeAppender(final Appender appender) {
608+
if (!isFullCompatibilityEnabled()) {
609+
return;
610+
}
596611
if (appender == null || aai == null) {
597612
return;
598613
}
@@ -611,6 +626,9 @@ public void removeAppender(final Appender appender) {
611626
*/
612627
@Override
613628
public void removeAppender(final String name) {
629+
if (!isFullCompatibilityEnabled()) {
630+
return;
631+
}
614632
if (name == null || aai == null) {
615633
return;
616634
}
@@ -622,6 +640,9 @@ public void removeAppender(final String name) {
622640
}
623641

624642
public void setAdditivity(final boolean additivity) {
643+
if (!isFullCompatibilityEnabled()) {
644+
return;
645+
}
625646
if (LogManager.isLog4jCorePresent()) {
626647
CategoryUtil.setAdditivity(logger, additivity);
627648
}
@@ -635,6 +656,9 @@ final void setHierarchy(final LoggerRepository repository) {
635656
}
636657

637658
public void setLevel(final Level level) {
659+
if (!isFullCompatibilityEnabled()) {
660+
return;
661+
}
638662
setLevel(level != null ? level.getVersion2Level() : null);
639663
}
640664

@@ -645,10 +669,16 @@ private void setLevel(final org.apache.logging.log4j.Level level) {
645669
}
646670

647671
public void setPriority(final Priority priority) {
672+
if (!isFullCompatibilityEnabled()) {
673+
return;
674+
}
648675
setLevel(priority != null ? priority.getVersion2Level() : null);
649676
}
650677

651678
public void setResourceBundle(final ResourceBundle bundle) {
679+
if (!isFullCompatibilityEnabled()) {
680+
return;
681+
}
652682
this.bundle = bundle;
653683
}
654684

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.apache.log4j.spi.NOPLoggerRepository;
2727
import org.apache.log4j.spi.RepositorySelector;
2828
import org.apache.log4j.spi.RootLogger;
29+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
2930
import org.apache.logging.log4j.spi.LoggerContext;
31+
import org.apache.logging.log4j.util.PropertiesUtil;
3032
import org.apache.logging.log4j.util.StackLocatorUtil;
3133

3234
/**
@@ -64,6 +66,10 @@ public final class LogManager {
6466

6567
private static final boolean LOG4J_CORE_PRESENT;
6668

69+
private static boolean isFullCompatibilityEnabled() {
70+
return PropertiesUtil.getProperties().getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL);
71+
}
72+
6773
static {
6874
LOG4J_CORE_PRESENT = checkLog4jCore();
6975
// By default, we use a DefaultRepositorySelector which always returns 'hierarchy'.
@@ -201,6 +207,9 @@ static void reconfigure(final ClassLoader classLoader) {
201207
}
202208

203209
public static void resetConfiguration() {
210+
if (!isFullCompatibilityEnabled()) {
211+
return;
212+
}
204213
resetConfiguration(StackLocatorUtil.getCallerClassLoader(2));
205214
}
206215

@@ -225,6 +234,9 @@ public static void setRepositorySelector(final RepositorySelector selector, fina
225234
* Shuts down the current configuration.
226235
*/
227236
public static void shutdown() {
237+
if (!isFullCompatibilityEnabled()) {
238+
return;
239+
}
228240
shutdown(StackLocatorUtil.getCallerClassLoader(2));
229241
}
230242

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

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

19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -27,11 +28,14 @@
2728
import java.util.Collections;
2829
import java.util.Enumeration;
2930
import java.util.List;
31+
import java.util.Locale;
3032
import java.util.Map;
33+
import java.util.ResourceBundle;
3134
import java.util.function.Consumer;
3235
import org.apache.log4j.bridge.AppenderAdapter;
3336
import org.apache.log4j.bridge.AppenderWrapper;
3437
import org.apache.log4j.spi.LoggingEvent;
38+
import org.apache.log4j.varia.NullAppender;
3539
import org.apache.logging.log4j.core.Layout;
3640
import org.apache.logging.log4j.core.LogEvent;
3741
import org.apache.logging.log4j.core.LoggerContext;
@@ -42,6 +46,7 @@
4246
import org.apache.logging.log4j.message.Message;
4347
import org.apache.logging.log4j.message.ObjectMessage;
4448
import org.apache.logging.log4j.message.SimpleMessage;
49+
import org.apache.logging.log4j.test.junit.SetTestProperty;
4550
import org.apache.logging.log4j.util.Constants;
4651
import org.apache.logging.log4j.util.Strings;
4752
import org.junit.jupiter.api.AfterAll;
@@ -52,6 +57,7 @@
5257
/**
5358
* Tests of Category.
5459
*/
60+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
5561
class CategoryTest {
5662

5763
static ConfigurationFactory cf = new BasicConfigurationFactory();
@@ -409,6 +415,72 @@ void testGetAppender() {
409415
}
410416
}
411417

418+
@Test
419+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
420+
void testSetLevelCompatibilityDisabled() {
421+
final Category category = Category.getInstance("TestCategory");
422+
final Level initialLevel = category.getEffectiveLevel();
423+
category.setLevel(Level.FATAL);
424+
assertEquals(initialLevel, category.getEffectiveLevel(), "level should be unchanged when compatibility is off");
425+
}
426+
427+
@Test
428+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
429+
void testSetAdditivityCompatibilityDisabled() {
430+
final Category category = Category.getInstance("TestCategory");
431+
final boolean initialAdditivity = category.getAdditivity();
432+
category.setAdditivity(!initialAdditivity);
433+
assertEquals(
434+
initialAdditivity,
435+
category.getAdditivity(),
436+
"additivity should be unchanged when compatibility is off");
437+
}
438+
439+
@Test
440+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
441+
void testAddAppenderCompatibilityDisabled() {
442+
final Category category = Category.getInstance("TestCategory");
443+
category.addAppender(new NullAppender());
444+
assertFalse(
445+
category.getAllAppenders().hasMoreElements(), "no appenders should be added when compatibility is off");
446+
}
447+
448+
@Test
449+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
450+
void testSetPriorityCompatibilityDisabled() {
451+
final Category category = Category.getInstance("TestCategory");
452+
final Priority initialPriority = category.getPriority();
453+
category.setPriority(Level.FATAL);
454+
assertEquals(initialPriority, category.getPriority(), "priority should be unchanged when compatibility is off");
455+
}
456+
457+
@Test
458+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
459+
void testSetResourceBundleCompatibilityDisabled() {
460+
final Category category = Category.getInstance("TestCategory");
461+
final ResourceBundle bundle = ResourceBundle.getBundle("L7D", new Locale("en", "US"));
462+
category.setResourceBundle(bundle);
463+
assertNull(category.getResourceBundle(), "resource bundle should not be set when compatibility is off");
464+
}
465+
466+
@Test
467+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
468+
void testRemoveAppenderCompatibilityDisabled() {
469+
final Category category = Category.getInstance("TestCategory");
470+
assertDoesNotThrow(
471+
() -> category.removeAppender("TestAppender"),
472+
"removeAppender should be a no-op and not throw exceptions when compatibility is off");
473+
}
474+
475+
@Test
476+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
477+
void testRemoveAllAppendersCompatibilityDisabled() {
478+
final Category category = Category.getInstance("TestCategory");
479+
assertDoesNotThrow(
480+
category::removeAllAppenders,
481+
"removeAllAppenders should be a no-op and not throw exceptions when compatibility is off");
482+
}
483+
412484
/**
413485
* Derived category to check method signature of forcedLog.
414486
*/

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
*/
1717
package org.apache.log4j;
1818

19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1920
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

2122
import java.util.Collections;
2223
import java.util.Enumeration;
2324
import java.util.List;
2425
import java.util.stream.Collectors;
26+
import org.apache.logging.log4j.test.junit.SetTestProperty;
2527
import org.junit.jupiter.api.Test;
2628

2729
/**
2830
* Tests {@link LogManager}.
2931
*/
32+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
3033
class LogManagerTest {
3134

3235
private static final String SIMPLE_NAME = LogManagerTest.class.getSimpleName();
@@ -47,4 +50,20 @@ void testGetCurrentLoggers() {
4750
assertTrue(names.contains(SIMPLE_NAME + ".foo"));
4851
assertTrue(names.contains(SIMPLE_NAME + ".foo.bar"));
4952
}
53+
54+
@Test
55+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
56+
void testResetConfigurationCompatibilityDisabled() {
57+
assertDoesNotThrow(
58+
() -> LogManager.resetConfiguration(),
59+
"resetConfiguration should be a no-op and not throw exceptions when compatibility is off");
60+
}
61+
62+
@Test
63+
@SetTestProperty(key = "log4j1.compatibility", value = "false")
64+
void testShutdownCompatibilityDisabled() {
65+
assertDoesNotThrow(
66+
() -> LogManager.shutdown(),
67+
"shutdown should be a no-op and not throw exceptions when compatibility is off");
68+
}
5069
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.logging.log4j.core.config.Property;
3939
import org.apache.logging.log4j.core.layout.PatternLayout;
4040
import org.apache.logging.log4j.core.test.appender.ListAppender;
41+
import org.apache.logging.log4j.test.junit.SetTestProperty;
4142
import org.junit.jupiter.api.AfterAll;
4243
import org.junit.jupiter.api.BeforeAll;
4344
import org.junit.jupiter.api.BeforeEach;
@@ -46,6 +47,7 @@
4647
/**
4748
* Used for internal unit testing the Logger class.
4849
*/
50+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
4951
class LoggerTest {
5052

5153
Appender a1;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns="https://logging.apache.org/xml/ns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
https://logging.apache.org/xml/ns
6+
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
7+
type="fixed">
8+
<issue id="3667" link="https://github.com/apache/logging-log4j2/issues/3667"/>
9+
<description format="asciidoc">
10+
Programmatic configuration changes using the Log4j 1.x API now respect the `log4j1.compatibility` flag.
11+
</description>
12+
</entry>

0 commit comments

Comments
 (0)