Skip to content

Logger context properties #1473

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

Merged
merged 25 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
405d65c
Interim commit
rgoers May 1, 2023
19bab40
More fixes
rgoers May 6, 2023
563ef53
Everything builds
rgoers May 7, 2023
afca580
Fix `PatternLayout.Builder#withHeader()` javadoc (#1411)
mernst Apr 10, 2023
9ae3c42
Fix `SimpleDateFormat`-related JDK bug URL in `FastDateParser` javado…
mernst Apr 10, 2023
eb61215
Ensure `FileOutputStream` is closed in `CommonsCompressAction.execute…
mernst Apr 11, 2023
06018e8
Fix nullability docs in `StrSubstitutor` (#1422)
mernst Apr 11, 2023
4145b93
Change `{@literal null}` to `{@code null}` (#1424)
mernst Apr 12, 2023
5df4462
Remove deprecated docs from `PatternParser#extractConverter()` (#1423)
mernst Apr 12, 2023
5252c22
Bump actions/checkout from 3.4.0 to 3.5.2 (#1434)
dependabot[bot] Apr 14, 2023
de92730
Bump actions/setup-python from 4.3.0 to 4.6.0 (#1440)
dependabot[bot] Apr 21, 2023
ec4d308
LOG4J2-3660 Fix `%notEmpty` for empty MDC/NDC inputs
vy Apr 23, 2023
b01b35b
LOG4J2-3660 Add `MdcPatternConverter` fixes missing from the cherry-pick
vy Apr 23, 2023
726eff8
Prepare for formatting of all files
ppkarwasz Apr 19, 2023
054dfb6
Apply Spotless
Apr 25, 2023
13be17d
Bump github/codeql-action from 2.1.28 to 2.3.1 (#1452)
dependabot[bot] Apr 27, 2023
6777e69
Bump github/codeql-action from 2.3.1 to 2.3.2 (#1455)
dependabot[bot] Apr 28, 2023
6bbbc46
Add Log4j Transform documentation
ppkarwasz Apr 28, 2023
a6b0f92
Fix license header
ppkarwasz Apr 28, 2023
9b551f6
Fix typo in `SECURITY.md` (#1457)
scovetta May 1, 2023
67b6980
Bump logging-parent from 8 to 9
ppkarwasz Apr 19, 2023
215ada5
Rebase with main
rgoers May 7, 2023
792237c
Trying to rebase from main
rgoers May 7, 2023
2eeb4c3
Provide LoggerContext properties during configuration
rgoers May 9, 2023
5e62903
merge (again) from main
rgoers May 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.log4j.spi.LoggingEvent;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.message.MapMessage;
Expand Down Expand Up @@ -59,13 +59,14 @@ public class CategoryTest {
public static void setupClass() {
appender.start();
version1Appender.setName(VERSION1_APPENDER_NAME);
System.setProperty(ConfigurationFactory.CONFIGURATION_FACTORY_PROPERTY, BasicConfigurationFactory.class.getName());
System.setProperty(Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey(),
BasicConfigurationFactory.class.getName());
}

@AfterAll
public static void cleanupClass() {
appender.stop();
System.clearProperty(ConfigurationFactory.CONFIGURATION_FACTORY_PROPERTY);
System.clearProperty(Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey());
}

@BeforeEach
Expand Down
7 changes: 4 additions & 3 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 @@ -24,8 +24,8 @@
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.Property;
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -66,12 +66,13 @@ public static void setUpClass() {
rbCH = ResourceBundle.getBundle("L7D", new Locale("fr", "CH"));
assertNotNull(rbCH, "Got a null resource bundle.");

System.setProperty(ConfigurationFactory.CONFIGURATION_FACTORY_PROPERTY, BasicConfigurationFactory.class.getName());
System.setProperty(Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey(),
BasicConfigurationFactory.class.getName());
}

@AfterAll
public static void tearDownClass() {
System.clearProperty(ConfigurationFactory.CONFIGURATION_FACTORY_PROPERTY);
System.clearProperty(Log4jPropertyKey.CONFIG_CONFIGURATION_FACTORY_CLASS_NAME.getSystemKey());
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class PropertiesConfigurationFactoryTest {

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class XmlConfigurationFactoryTest {

@BeforeClass
public static void beforeClass() {
System.setProperty(ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY, "target/test-classes/log4j1-file.xml");
System.setProperty(ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY.getSystemKey(),
"target/test-classes/log4j1-file.xml");
}
@Test
public void testXML() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.test.junit;


import org.apache.logging.log4j.test.TestProperties;
import org.apache.logging.log4j.util.PropertySource;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.logging.log4j;

import org.apache.logging.log4j.spi.LoggingSystemProperties;
import org.apache.logging.log4j.spi.LoggingSystemProperty;
import org.apache.logging.log4j.test.junit.InitializesThreadContext;
import org.apache.logging.log4j.test.junit.UsingThreadContextMap;
import org.junit.jupiter.api.Test;
Expand All @@ -30,8 +30,8 @@
public class NoopThreadContextTest {

@Test
@SetSystemProperty(key = LoggingSystemProperties.THREAD_CONTEXT_DISABLED, value = "true")
@SetSystemProperty(key = LoggingSystemProperties.THREAD_CONTEXT_MAP_DISABLED, value = "true")
@SetSystemProperty(key = LoggingSystemProperty.Constant.THREAD_CONTEXT_ENABLED, value = "false")
@SetSystemProperty(key = LoggingSystemProperty.Constant.THREAD_CONTEXT_MAP_ENABLED, value = "false")
@InitializesThreadContext
@UsingThreadContextMap
public void testNoop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j;

import org.apache.logging.log4j.spi.LoggingSystemProperty;
import org.apache.logging.log4j.test.ThreadContextUtilityClass;
import org.apache.logging.log4j.test.junit.InitializesThreadContext;
import org.apache.logging.log4j.test.junit.UsingThreadContextMap;
Expand All @@ -24,13 +25,12 @@
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;

import static org.apache.logging.log4j.spi.LoggingSystemProperties.THREAD_CONTEXT_MAP_INHERITABLE;
import static org.junit.jupiter.api.Assertions.*;

/**
* Tests {@link ThreadContext}.
*/
@SetSystemProperty(key = THREAD_CONTEXT_MAP_INHERITABLE, value = "true")
@SetSystemProperty(key = LoggingSystemProperty.Constant.THREAD_CONTEXT_MAP_INHERITABLE, value = "true")
@InitializesThreadContext
public class ThreadContextInheritanceTest {

Expand All @@ -45,7 +45,7 @@ public void testPush() {
}

@Test
@SetSystemProperty(key = THREAD_CONTEXT_MAP_INHERITABLE, value = "true")
@SetSystemProperty(key = LoggingSystemProperty.Constant.THREAD_CONTEXT_MAP_INHERITABLE, value = "true")
@InitializesThreadContext
public void testInheritanceSwitchedOn() throws Exception {
ThreadContext.put("Greeting", "Hello");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public void testInjectedFields() {

// Test that per-class properties are overridden by per-test properties
final PropertyEnvironment env = PropertiesUtil.getProperties();
staticProperties.setProperty("log4j2.staticProperty", "static");
staticProperties.setProperty("log4j2.instanceProperty", "static");
instanceProperties.setProperty("log4j2.instanceProperty", "instance");
assertThat(env.getStringProperty("log4j2.staticProperty")).isEqualTo("static");
assertThat(env.getStringProperty("log4j.instanceProperty")).isEqualTo("instance");
staticProperties.setProperty("static.Property", "static");
staticProperties.setProperty("instance.Property", "static");
instanceProperties.setProperty("instance.Property", "instance");
assertThat(env.getStringProperty("static.Property")).isEqualTo("static");
assertThat(env.getStringProperty("instance.Property")).isEqualTo("instance");
}

@Test
Expand All @@ -49,9 +49,9 @@ public void testInjectedParameter(final TestProperties paramProperties) {
}

@Test
@SetTestProperty(key = "log4j2.testSetTestProperty", value = "true")
@SetTestProperty(key = "test.SetTestProperty", value = "true")
public void testSetTestProperty() {
final PropertyEnvironment env = PropertiesUtil.getProperties();
assertThat(env.getBooleanProperty("log4j2.testSetTestProperty")).isTrue();
assertThat(env.getBooleanProperty("test.SetTestProperty")).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class EnvironmentPropertySourceTest {

public static Object[][] data() {
return new Object[][]{
{"LOG4J_CONFIGURATION_FILE", Arrays.asList("configuration", "file")},
{"LOG4J_FOO_BAR_PROPERTY", Arrays.asList("foo", "bar", "property")},
{"LOG4J_EXACT", Collections.singletonList("EXACT")},
{"LOG4J_TEST_PROPERTY_NAME", PropertySource.Util.tokenize("Log4jTestPropertyName")},
{"CONFIGURATION_FILE", Arrays.asList("CONFIGURATION", "FILE")},
{"LOG4J2_FOO_BAR_PROPERTY", Arrays.asList("LOG4J2", "FOO", "BAR", "PROPERTY")},
{"LOG4J2_EXACT", Arrays.asList("LOG4J2", "EXACT")},
{"LOG4J2_TEST_PROPERTY_NAME", Arrays.asList("LOG4J2", "TEST", "PROPERTY", "NAME")},
{null, Collections.emptyList()}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,67 +27,59 @@ public class LegacyPropertiesCompatibilityTest {

public static Object[][] data() {
return new Object[][]{
{"log4j2.configurationFile", "log4j.configurationFile"},
{"log4j2.mergeStrategy", "log4j.mergeStrategy"},
{"log4j2.contextSelector", "Log4jContextSelector"},
{"log4j2.logEventFactory", "Log4jLogEventFactory"},
{"log4j2.configurationFactory", "log4j.configurationFactory"},
{"log4j2.shutdownCallbackRegistry", "log4j.shutdownCallbackRegistry"},
{"log4j2.clock", "log4j.Clock"},
{"log4j2.level", "org.apache.logging.log4j.level"},
{"log4j2.disableThreadContext", "disableThreadContext"},
{"log4j2.disableThreadContextStack", "disableThreadContextStack"},
{"log4j2.disableThreadContextMap", "disableThreadContextMap"},
{"log4j2.isThreadContextMapInheritable", "isThreadContextMapInheritable"},
{"log4j2.contextDataInjector", "log4j2.ContextDataInjector"},
{"log4j2.garbagefreeThreadContextMap", "log4j2.garbagefree.threadContextMap"},
{"log4j2.disableJmx", "log4j2.disable.jmx"},
{"log4j2.jmxNotifyAsync", "log4j2.jmx.notify.async"},
{"log4j2.skipJansi", "log4j.skipJansi"},
{"log4j2.ignoreTCL", "log4j.ignoreTCL"},
{"log4j2.ignoreTCL", "LOG4J_IGNORE_TCL"}, // just a sanity check for fun camel case names
{"log4j2.uuidSequence", "org.apache.logging.log4j.uuidSequence"},
{"log4j2.assignedSequences", "org.apache.logging.log4j.assignedSequences"},
{"log4j2.simplelogShowContextMap", "org.apache.logging.log4j.simplelog.showContextMap"},
{"log4j2.simplelogShowlogname", "org.apache.logging.log4j.simplelog.showlogname"},
{"log4j2.simplelogShowShortLogname", "org.apache.logging.log4j.simplelog.showShortLogname"},
{"log4j2.simplelogShowdatetime", "org.apache.logging.log4j.simplelog.showdatetime"},
{"log4j2.simplelogDateTimeFormat", "org.apache.logging.log4j.simplelog.dateTimeFormat"},
{"log4j2.simplelogLogFile", "org.apache.logging.log4j.simplelog.logFile"},
{"log4j2.simplelogLevel", "org.apache.logging.log4j.simplelog.level"},
{"log4j2.simplelog.com.foo.bar.Thing.level", "org.apache.logging.log4j.simplelog.com.foo.bar.Thing.level"},
{"log4j2.simplelogComFooBarThingLevel", "org.apache.logging.log4j.simplelog.com.foo.bar.Thing.level"},
{"log4j2.simplelogStatusLoggerLevel", "org.apache.logging.log4j.simplelog.StatusLogger.level"},
{"log4j2.defaultStatusLevel", "Log4jDefaultStatusLevel"},
{"log4j2.statusLoggerLevel", "log4j2.StatusLogger.level"},
{"log4j2.statusEntries", "log4j2.status.entries"},
{"log4j2.asyncLoggerExceptionHandler", "AsyncLogger.ExceptionHandler"},
{"log4j2.asyncLoggerRingBufferSize", "AsyncLogger.RingBufferSize"},
{"log4j2.asyncLoggerWaitStrategy", "AsyncLogger.WaitStrategy"},
{"log4j2.asyncLoggerThreadNameStrategy", "AsyncLogger.ThreadNameStrategy"},
{"log4j2.asyncLoggerConfigExceptionHandler", "AsyncLoggerConfig.ExceptionHandler"},
{"log4j2.asyncLoggerConfigRingBufferSize", "AsyncLoggerConfig.RingBufferSize"},
{"log4j2.asyncLoggerConfigWaitStrategy", "AsyncLoggerConfig.WaitStrategy"},
{"log4j2.julLoggerAdapter", "log4j.jul.LoggerAdapter"},
{"log4j2.formatMsgAsync", "log4j.format.msg.async"},
{"log4j2.asyncQueueFullPolicy", "log4j2.AsyncQueueFullPolicy"},
{"log4j2.discardThreshold", "log4j2.DiscardThreshold"},
{"log4j2.isWebapp", "log4j2.is.webapp"},
{"log4j2.enableThreadlocals", "log4j2.enable.threadlocals"},
{"log4j2.enableDirectEncoders", "log4j2.enable.direct.encoders"},
{"log4j2.initialReusableMsgSize", "log4j.initialReusableMsgSize"},
{"log4j2.maxReusableMsgSize", "log4j.maxReusableMsgSize"},
{"log4j2.layoutStringBuilderMaxSize", "log4j.layoutStringBuilder.maxSize"},
{"log4j2.unboxRingbufferSize", "log4j.unbox.ringbuffer.size"},
{"log4j2.loggerContextStacktraceOnStart", "log4j.LoggerContext.stacktrace.on.start"},
{"log4j2.configurationFile", "Configuration.file"},
{"log4j2.mergeStrategy", "Configuration.mergeStrategy"},
{"log4j2.contextSelector", "LoggerContext.selector"},
{"log4j2.logEventFactory", "Logger.logEventFactory"},
{"log4j2.configurationFactory", "Configuration.factory"},
{"log4j2.shutdownCallbackRegistry", "LoggerContext.shutdownCallbackRegistry"},
{"log4j2.clock", "Configuration.clock"},
{"log4j2.level", "Configuration.level"},
{"log4j2.isThreadContextMapInheritable", "ThreadContext.inheritable"},
{"log4j2.contextDataInjector", "ThreadContext.contextDataInjector"},
{"log4j2.garbagefreeThreadContextMap", "ThreadContext.garbageFreeMap"},
{"log4j2.jmxNotifyAsync", "JMX.notifyAsync"},
{"log4j2.ignoreTCL", "Loader.ignoreTCL"},
{"log4j2.uuidSequence", "UUID.sequence"},
{"log4j2.assignedSequences", "UUID.assignedSequences"},
{"log4j2.simplelogShowContextMap", "SimpleLogger.showContextMap"},
{"log4j2.simplelogShowlogname", "SimpleLogger.showLogName"},
{"log4j2.simplelogShowShortLogname", "SimpleLogger.showShortLogName"},
{"log4j2.simplelogShowdatetime", "SimpleLogger.showDateTime"},
{"log4j2.simplelogDateTimeFormat", "SimpleLogger.dateTimeFormat"},
{"log4j2.simplelogLogFile", "SimpleLogger.logFile"},
{"log4j2.simplelogLevel", "SimpleLogger.level"},
{"log4j2.simplelogStatusLoggerLevel", "SimpleLogger.statusLoggerLevel"},
{"log4j2.defaultStatusLevel", "StatusLogger.defaultLevel"},
{"log4j2.statusLoggerLevel", "StatusLogger.level"},
{"log4j2.statusEntries", "StatusLogger.entries"},
{"log4j2.asyncLoggerExceptionHandler", "AsyncLogger.exceptionHandler"},
{"log4j2.asyncLoggerRingBufferSize", "AsyncLogger.ringBufferSize"},
{"log4j2.asyncLoggerWaitStrategy", "AsyncLogger.waitStrategy"},
{"log4j2.asyncLoggerThreadNameStrategy", "AsyncLogger.threadNameStrategy"},
{"log4j2.asyncLoggerConfigExceptionHandler", "AsyncLoggerConfig.exceptionHandler"},
{"log4j2.asyncLoggerConfigRingBufferSize", "AsyncLoggerConfig.ringBufferSize"},
{"log4j2.asyncLoggerConfigWaitStrategy", "AsyncLoggerConfig.waitStrategy"},
{"log4j2.julLoggerAdapter", "JUL.loggerAdapter"},
{"log4j2.formatMsgAsync", "AsyncLogger.formatMsg"},
{"log4j2.asyncQueueFullPolicy", "AsyncLogger.queueFullPolicy"},
{"log4j2.discardThreshold", "AsyncLogger.discardThreshold"},
{"log4j2.isWebapp", "Web.isWebApp"},
{"log4j2.enableThreadlocals", "ThreadLocals.enable"},
{"log4j2.enableDirectEncoders", "GC.enableDirectEncoders"},
{"log4j2.initialReusableMsgSize", "GC.initialReusableMsgSize"},
{"log4j2.maxReusableMsgSize", "GC.maxReusableMsgSize"},
{"log4j2.layoutStringBuilderMaxSize", "GC.layoutStringBuilderMaxSize"},
{"log4j2.unboxRingbufferSize", "Unbox.ringBufferSize"},
{"log4j2.loggerContextStacktraceOnStart", "LoggerContext.stackTraceOnStart"}
};
}

@ParameterizedTest
@MethodSource("data")
public void compareNewWithOldName(final String newName, final String oldName) {
public void compareNewWithOldName(final String oldName, final String newName) {
final List<CharSequence> newTokens = PropertySource.Util.tokenize(newName);
final List<CharSequence> oldTokens = PropertySource.Util.tokenize(oldName);
assertEquals(oldTokens, newTokens);
assertEquals(newTokens, oldTokens);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class PropertiesPropertySourceTest {

public static Object[][] data() {
return new Object[][]{
{"log4j2.configurationFile", Arrays.asList("configuration", "file")},
{"log4j2.fooBarProperty", Arrays.asList("foo", "bar", "property")},
{"log4j2.EXACT", Collections.singletonList("EXACT")},
{"log4j2.testPropertyName", PropertySource.Util.tokenize("Log4jTestPropertyName")},
{"log4j2.configuration.file", Arrays.asList("configuration", "file")},
{"log4j2.foo.bar.property", Arrays.asList("foo", "bar", "property")},
{"log4j2.LOWER", Collections.singletonList("LOWER")},
{"log4j2.Log4jTestPropertyName", PropertySource.Util.tokenize("Log4jTestPropertyName")},
{null, Collections.emptyList()}
};
}
Expand Down
Loading