Skip to content

Commit

Permalink
Replace PropertiesUtil with PropertyEnvironment
Browse files Browse the repository at this point in the history
This replaces `PropertiesUtil` with `PropertyEnvironment` in modules
that depend on Log4j Core.

It also ensures that every logger context has a different version of
`PropertyEnvironment`.
  • Loading branch information
ppkarwasz committed Mar 22, 2024
1 parent 68e22a7 commit c3f4261
Show file tree
Hide file tree
Showing 178 changed files with 1,797 additions and 1,729 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Reconfigurable;
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
import org.apache.logging.log4j.plugins.di.ConfigurableInstanceFactory;
import org.apache.logging.log4j.plugins.di.DI;
import org.apache.logging.log4j.util.PropertiesUtil;

/**
* Base Configuration for Log4j 1.
Expand Down Expand Up @@ -58,7 +58,7 @@ public Log4j1Configuration(
configurationSource,
Optional.ofNullable(loggerContext)
.map(LoggerContext::getEnvironment)
.orElseGet(PropertiesUtil::getProperties),
.orElseGet(PropertyEnvironment::getGlobal),
Optional.ofNullable(loggerContext)
.map(ctx -> (ConfigurableInstanceFactory) ctx.getInstanceFactory())
.orElseGet(DI::createInitializedFactory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Order;
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
import org.apache.logging.log4j.plugins.Namespace;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.util.PropertiesUtil;

/**
* Configures Log4j from a log4j 1 format properties file.
Expand All @@ -45,18 +45,26 @@ public class PropertiesConfigurationFactory extends ConfigurationFactory {
*/
protected static final String DEFAULT_PREFIX = "log4j";

private final boolean enabled;

public PropertiesConfigurationFactory() {
this(PropertyEnvironment.getGlobal());
}

private PropertiesConfigurationFactory(final PropertyEnvironment props) {
this.enabled = props.getBooleanProperty(LOG4J1_EXPERIMENTAL)
|| props.getStringProperty(LOG4J1_CONFIGURATION_FILE_PROPERTY) != null;
}

@Override
protected String[] getSupportedTypes() {
if (!PropertiesUtil.getProperties()
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE)) {
return null;
}
return new String[] {FILE_EXTENSION};
return enabled ? new String[] {FILE_EXTENSION} : null;
}

@Override
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
final int interval =
PropertyEnvironment.getGlobal().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
return new PropertiesConfiguration(loggerContext, source, interval);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ private static String substVars(final String val, final Properties props, final
j += DELIM_START_LEN;
final String key = val.substring(j, k);
// first try in System properties
// We intentionally use PropertiesUtil here to retrieve properties not
// prefixed by "log4j."
String replacement = PropertiesUtil.getProperties().getStringProperty(key, null);
// then try props parameter
if (replacement == null && props != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Order;
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
import org.apache.logging.log4j.plugins.Namespace;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.util.PropertiesUtil;

/**
* Constructs a Configuration usable in Log4j 2 from a Log4j 1 configuration file.
Expand All @@ -46,18 +46,26 @@ public class XmlConfigurationFactory extends ConfigurationFactory {
*/
protected static final String DEFAULT_PREFIX = "log4j";

private final boolean enabled;

public XmlConfigurationFactory() {
this(PropertyEnvironment.getGlobal());
}

private XmlConfigurationFactory(final PropertyEnvironment props) {
this.enabled = props.getBooleanProperty(LOG4J1_CONFIGURATION_FILE_PROPERTY)
|| props.getStringProperty(LOG4J1_CONFIGURATION_FILE_PROPERTY) != null;
}

@Override
protected String[] getSupportedTypes() {
if (!PropertiesUtil.getProperties()
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE)) {
return null;
}
return new String[] {FILE_EXTENSION};
return enabled ? new String[] {FILE_EXTENSION} : null;
}

@Override
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
final int interval =
PropertyEnvironment.getGlobal().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
return new XmlConfiguration(loggerContext, source, interval);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@
import org.apache.log4j.bridge.AppenderAdapter;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;

/**
* Test configuration from XML.
*/
public class AutoConfigTest {

@Test
@LegacyLoggerContextSource("log4j.xml")
public void testListAppender(final org.apache.logging.log4j.core.LoggerContext context) {
@SetSystemProperty(key = "log4j.configuration", value = "log4j.xml")
public void testListAppender() {
final Logger logger = LogManager.getLogger("test");
final LoggerContext context = LoggerContext.getContext(false);
logger.debug("This is a test of the root logger");
final Configuration configuration = context.getConfiguration();
final Map<String, Appender> appenders = configuration.getAppenders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.log4j.config;

import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
Expand All @@ -33,9 +34,9 @@
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingThreadContextMap;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;

/**
* Test RewriteAppender
Expand All @@ -44,7 +45,7 @@
public class MapRewriteAppenderTest {

@Test
@LegacyLoggerContextSource("log4j1-mapRewrite.xml")
@SetSystemProperty(key = LOG4J1_CONFIGURATION_FILE_PROPERTY, value = "log4j1-mapRewrite.xml")
public void testRewrite() {
final Logger logger = LogManager.getLogger("test");
final Map<String, String> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package org.apache.log4j.config;

import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
import static org.junit.Assert.assertTrue;

import java.io.File;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -32,9 +32,7 @@ public class PropertiesConfigurationFactoryTest {

@BeforeClass
public static void beforeClass() {
System.setProperty(
ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY.getSystemKey(),
"target/test-classes/log4j1-file-1.properties");
System.setProperty(LOG4J1_CONFIGURATION_FILE_PROPERTY, "target/test-classes/log4j1-file-1.properties");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package org.apache.log4j.config;

import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Paths;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
Expand All @@ -35,10 +35,10 @@ public class PropertiesRollingWithPropertiesTest {

@Test
@SetSystemProperty(key = "test.directory", value = TEST_DIR)
@SetSystemProperty(key = LOG4J1_CONFIGURATION_FILE_PROPERTY, value = "log4j1-rolling-properties.properties")
@CleanUpDirectories(TEST_DIR)
@LegacyLoggerContextSource("log4j1-rolling-properties.properties")
public void testProperties(final LoggerContext context) throws Exception {
final Logger logger = context.getLogger("test");
public void testProperties() throws Exception {
final Logger logger = LoggerContext.getContext(false).getLogger("test");
logger.debug("This is a test of the root logger");
assertThat(Paths.get(TEST_DIR, "somefile.log")).exists().isNotEmptyFile();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.log4j.config;

import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -31,9 +32,9 @@
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingThreadContextMap;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;

/**
* Test RewriteAppender
Expand All @@ -42,9 +43,10 @@
public class RewriteAppenderTest {

@Test
@LegacyLoggerContextSource("log4j1-rewrite.xml")
public void testRewrite(final LoggerContext context) {
@SetSystemProperty(key = LOG4J1_CONFIGURATION_FILE_PROPERTY, value = "log4j1-rewrite.xml")
public void testRewrite() {
final Logger logger = LogManager.getLogger("test");
final LoggerContext context = LoggerContext.getContext(false);
ThreadContext.put("key1", "This is a test");
ThreadContext.put("hello", "world");
final long logTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package org.apache.log4j.config;

import static org.apache.logging.log4j.core.impl.Log4jPropertyKey.CONFIG_V1_FILE_NAME;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.test.TestConstants;
import org.apache.logging.log4j.core.test.net.mock.MockSyslogServer;
import org.apache.logging.log4j.core.test.net.mock.MockSyslogServerFactory;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -42,12 +42,12 @@ public class SyslogAppenderTest {
public static void beforeClass() throws IOException {
initTCPTestEnvironment();
System.setProperty("syslog.port", Integer.toString(syslogServer.getLocalPort()));
System.setProperty(CONFIG_V1_FILE_NAME.getKey(), "target/test-classes/log4j1-syslog.xml");
System.setProperty(TestConstants.VERSION1_CONFIGURATION, "target/test-classes/log4j1-syslog.xml");
}

@AfterAll
public static void cleanup() {
System.clearProperty(CONFIG_V1_FILE_NAME.getKey());
System.clearProperty(TestConstants.VERSION1_CONFIGURATION);
syslogServer.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.File;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -32,9 +31,7 @@ public class XmlConfigurationFactoryTest {

@BeforeClass
public static void beforeClass() {
System.setProperty(
ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY.getSystemKey(),
"target/test-classes/log4j1-file.xml");
System.setProperty("log4j2.Version1.configuration", "target/test-classes/log4j1-file.xml");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package org.apache.log4j.config;

import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Paths;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LegacyLoggerContextSource;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
Expand All @@ -35,11 +35,11 @@ public class XmlRollingWithPropertiesTest {

@Test
@SetSystemProperty(key = "test.directory", value = TEST_DIR)
@SetSystemProperty(key = LOG4J1_CONFIGURATION_FILE_PROPERTY, value = "log4j1-rolling-properties.xml")
@CleanUpDirectories(TEST_DIR)
@LegacyLoggerContextSource("log4j1-rolling-properties.xml")
public void testProperties(final LoggerContext context) {
public void testProperties() {
// ${test.directory}/logs/etl.log
final Logger logger = context.getLogger("test");
final Logger logger = LoggerContext.getContext(false).getLogger("test");
logger.debug("This is a test of the root logger");
assertThat(Paths.get(TEST_DIR, "logs/etl.log")).exists().isNotEmptyFile();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,37 @@
import org.junit.jupiter.api.parallel.ResourceLock;

/**
* Constants to use the the {@link ResourceLock} annotation.
*
* Constants to use the {@link ResourceLock} annotation.
*/
public class Resources {
public final class Resources {

/**
* Marks tests that require access to {@link org.apache.logging.log4j.LogManager} methods or change its
* underlying {@link org.apache.logging.log4j.spi.LoggerContextFactory} implementation.
*/
public static final String LOG_MANAGER = "log4j.LogManager";

/**
* Marks tests that require access to {@link org.apache.logging.log4j.ThreadContext} methods or change its
* underlying {@link org.apache.logging.log4j.spi.ThreadContextMap} implementation.
*/
public static final String THREAD_CONTEXT = "log4j.ThreadContext";

/**
* Marks tests that require access to {@link org.apache.logging.log4j.MarkerManager} methods.
*/
public static final String MARKER_MANAGER = "log4j.MarkerManager";

/**
* Marks tests that requires access to {@link org.apache.logging.log4j.Level} static methods to create new levels.
*/
public static final String LEVEL = "log4j.Level";

public static final String THREAD_CONTEXT = "log4j2.ThreadContext";
/**
* Marks tests that require access to {@link org.apache.logging.log4j.status.StatusLogger} static methods or
* change its underlying implementation.
*/
public static final String STATUS_LOGGER = "log4j.StatusLogger";

public static final String MARKER_MANAGER = "log4j2.MarkerManager";
private Resources() {}
}
Loading

0 comments on commit c3f4261

Please sign in to comment.