Skip to content

Respect log4j1.compatibility for programmatic configuration #3812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions log4j-1.2-api/src/main/java/org/apache/log4j/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
import org.apache.log4j.spi.HierarchyEventListener;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.message.LocalizedMessage;
import org.apache.logging.log4j.message.MapMessage;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ObjectMessage;
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.spi.LoggerContext;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.util.StackLocatorUtil;
import org.apache.logging.log4j.util.Strings;

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

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

private static boolean isFullCompatibilityEnabled() {
return PropertiesUtil.getProperties().getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL);
}

/**
* Tests if the named category exists (in the default hierarchy).
*
Expand Down Expand Up @@ -192,6 +198,9 @@ protected Category(final String name) {
*/
@Override
public void addAppender(final Appender appender) {
if (!isFullCompatibilityEnabled()) {
return;
}
if (appender != null) {
if (LogManager.isLog4jCorePresent()) {
CategoryUtil.addAppender(logger, AppenderAdapter.adapt(appender));
Expand Down Expand Up @@ -572,6 +581,9 @@ void maybeLog(
*/
@Override
public void removeAllAppenders() {
if (!isFullCompatibilityEnabled()) {
return;
}
if (aai != null) {
final Vector appenders = new Vector();
for (final Enumeration iter = aai.getAllAppenders(); iter != null && iter.hasMoreElements(); ) {
Expand All @@ -593,6 +605,9 @@ public void removeAllAppenders() {
*/
@Override
public void removeAppender(final Appender appender) {
if (!isFullCompatibilityEnabled()) {
return;
}
if (appender == null || aai == null) {
return;
}
Expand All @@ -611,6 +626,9 @@ public void removeAppender(final Appender appender) {
*/
@Override
public void removeAppender(final String name) {
if (!isFullCompatibilityEnabled()) {
return;
}
if (name == null || aai == null) {
return;
}
Expand All @@ -622,6 +640,9 @@ public void removeAppender(final String name) {
}

public void setAdditivity(final boolean additivity) {
if (!isFullCompatibilityEnabled()) {
return;
}
if (LogManager.isLog4jCorePresent()) {
CategoryUtil.setAdditivity(logger, additivity);
}
Expand All @@ -635,6 +656,9 @@ final void setHierarchy(final LoggerRepository repository) {
}

public void setLevel(final Level level) {
if (!isFullCompatibilityEnabled()) {
return;
}
setLevel(level != null ? level.getVersion2Level() : null);
}

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

public void setPriority(final Priority priority) {
if (!isFullCompatibilityEnabled()) {
return;
}
setLevel(priority != null ? priority.getVersion2Level() : null);
}

public void setResourceBundle(final ResourceBundle bundle) {
if (!isFullCompatibilityEnabled()) {
return;
}
this.bundle = bundle;
}

Expand Down
12 changes: 12 additions & 0 deletions log4j-1.2-api/src/main/java/org/apache/log4j/LogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.apache.log4j.spi.NOPLoggerRepository;
import org.apache.log4j.spi.RepositorySelector;
import org.apache.log4j.spi.RootLogger;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.spi.LoggerContext;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.util.StackLocatorUtil;

/**
Expand Down Expand Up @@ -64,6 +66,10 @@ public final class LogManager {

private static final boolean LOG4J_CORE_PRESENT;

private static boolean isFullCompatibilityEnabled() {
return PropertiesUtil.getProperties().getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL);
}

static {
LOG4J_CORE_PRESENT = checkLog4jCore();
// By default, we use a DefaultRepositorySelector which always returns 'hierarchy'.
Expand Down Expand Up @@ -201,6 +207,9 @@ static void reconfigure(final ClassLoader classLoader) {
}

public static void resetConfiguration() {
if (!isFullCompatibilityEnabled()) {
return;
}
resetConfiguration(StackLocatorUtil.getCallerClassLoader(2));
}

Expand All @@ -225,6 +234,9 @@ public static void setRepositorySelector(final RepositorySelector selector, fina
* Shuts down the current configuration.
*/
public static void shutdown() {
if (!isFullCompatibilityEnabled()) {
return;
}
shutdown(StackLocatorUtil.getCallerClassLoader(2));
}

Expand Down
72 changes: 72 additions & 0 deletions log4j-1.2-api/src/test/java/org/apache/log4j/CategoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.log4j;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
Expand All @@ -27,11 +28,14 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.Consumer;
import org.apache.log4j.bridge.AppenderAdapter;
import org.apache.log4j.bridge.AppenderWrapper;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.varia.NullAppender;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.LoggerContext;
Expand All @@ -42,6 +46,7 @@
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ObjectMessage;
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.apache.logging.log4j.util.Constants;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -52,6 +57,7 @@
/**
* Tests of Category.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class CategoryTest {

static ConfigurationFactory cf = new BasicConfigurationFactory();
Expand Down Expand Up @@ -409,6 +415,72 @@ void testGetAppender() {
}
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testSetLevelCompatibilityDisabled() {
final Category category = Category.getInstance("TestCategory");
final Level initialLevel = category.getEffectiveLevel();
category.setLevel(Level.FATAL);
assertEquals(initialLevel, category.getEffectiveLevel(), "level should be unchanged when compatibility is off");
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testSetAdditivityCompatibilityDisabled() {
final Category category = Category.getInstance("TestCategory");
final boolean initialAdditivity = category.getAdditivity();
category.setAdditivity(!initialAdditivity);
assertEquals(
initialAdditivity,
category.getAdditivity(),
"additivity should be unchanged when compatibility is off");
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testAddAppenderCompatibilityDisabled() {
final Category category = Category.getInstance("TestCategory");
category.addAppender(new NullAppender());
assertFalse(
category.getAllAppenders().hasMoreElements(), "no appenders should be added when compatibility is off");
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testSetPriorityCompatibilityDisabled() {
final Category category = Category.getInstance("TestCategory");
final Priority initialPriority = category.getPriority();
category.setPriority(Level.FATAL);
assertEquals(initialPriority, category.getPriority(), "priority should be unchanged when compatibility is off");
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testSetResourceBundleCompatibilityDisabled() {
final Category category = Category.getInstance("TestCategory");
final ResourceBundle bundle = ResourceBundle.getBundle("L7D", new Locale("en", "US"));
category.setResourceBundle(bundle);
assertNull(category.getResourceBundle(), "resource bundle should not be set when compatibility is off");
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testRemoveAppenderCompatibilityDisabled() {
final Category category = Category.getInstance("TestCategory");
assertDoesNotThrow(
() -> category.removeAppender("TestAppender"),
"removeAppender should be a no-op and not throw exceptions when compatibility is off");
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testRemoveAllAppendersCompatibilityDisabled() {
final Category category = Category.getInstance("TestCategory");
assertDoesNotThrow(
category::removeAllAppenders,
"removeAllAppenders should be a no-op and not throw exceptions when compatibility is off");
}

/**
* Derived category to check method signature of forcedLog.
*/
Expand Down
19 changes: 19 additions & 0 deletions log4j-1.2-api/src/test/java/org/apache/log4j/LogManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
*/
package org.apache.log4j;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.logging.log4j.test.junit.SetTestProperty;
import org.junit.jupiter.api.Test;

/**
* Tests {@link LogManager}.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class LogManagerTest {

private static final String SIMPLE_NAME = LogManagerTest.class.getSimpleName();
Expand All @@ -47,4 +50,20 @@ void testGetCurrentLoggers() {
assertTrue(names.contains(SIMPLE_NAME + ".foo"));
assertTrue(names.contains(SIMPLE_NAME + ".foo.bar"));
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testResetConfigurationCompatibilityDisabled() {
assertDoesNotThrow(
() -> LogManager.resetConfiguration(),
"resetConfiguration should be a no-op and not throw exceptions when compatibility is off");
}

@Test
@SetTestProperty(key = "log4j1.compatibility", value = "false")
void testShutdownCompatibilityDisabled() {
assertDoesNotThrow(
() -> LogManager.shutdown(),
"shutdown should be a no-op and not throw exceptions when compatibility is off");
}
}
2 changes: 2 additions & 0 deletions log4j-1.2-api/src/test/java/org/apache/log4j/LoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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.apache.logging.log4j.test.junit.SetTestProperty;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -46,6 +47,7 @@
/**
* Used for internal unit testing the Logger class.
*/
@SetTestProperty(key = "log4j1.compatibility", value = "true")
class LoggerTest {

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