You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2022/11/13 01:15:24 UTC
[logging-log4j2] branch master updated: Use lazy values for safer publication of thread context changes
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new 7e2716b42b Use lazy values for safer publication of thread context changes
7e2716b42b is described below
commit 7e2716b42b5b557ab2745dc7e2fe7e91fff78c2c
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Sat Nov 12 19:14:45 2022 -0600
Use lazy values for safer publication of thread context changes
Signed-off-by: Matt Sicker <ma...@apache.org>
---
.../apache/logging/log4j/spi/LoggingSystem.java | 30 +++++++++++-----------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggingSystem.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggingSystem.java
index 1228d030a5..6d98c42932 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggingSystem.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/LoggingSystem.java
@@ -72,7 +72,8 @@ public class LoggingSystem {
private final Lock initializationLock = new ReentrantLock();
private volatile SystemProvider provider;
private final Lazy<PropertyEnvironment> environmentLazy = Lazy.relaxed(PropertiesUtil::getProperties);
- private final Lazy<LoggerContextFactory> loggerContextFactoryLazy = Lazy.lazy(() -> getProvider().createLoggerContextFactory());
+ private final Lazy<LoggerContextFactory> loggerContextFactoryLazy = environmentLazy.map(environment ->
+ getProvider().createLoggerContextFactory(environment));
private final Lazy<MessageFactory> messageFactoryLazy = environmentLazy.map(environment -> {
final String className = environment.getStringProperty(LOGGER_MESSAGE_FACTORY_CLASS);
if (className != null) {
@@ -93,8 +94,10 @@ public class LoggingSystem {
}
return new DefaultFlowMessageFactory();
});
- private Supplier<ThreadContextMap> threadContextMapSupplier = () -> getProvider().createContextMap();
- private Supplier<ThreadContextStack> threadContextStackSupplier = () -> getProvider().createContextStack();
+ private final Lazy<Supplier<ThreadContextMap>> threadContextMapFactoryLazy = environmentLazy.map(environment ->
+ () -> getProvider().createContextMap(environment));
+ private final Lazy<Supplier<ThreadContextStack>> threadContextStackFactoryLazy = environmentLazy.map(environment ->
+ () -> getProvider().createContextStack(environment));
/**
* Acquires a lock on the initialization of locating a logging system provider. This lock should be
@@ -164,12 +167,12 @@ public class LoggingSystem {
flowMessageFactoryLazy.set(flowMessageFactory);
}
- public void setThreadContextMapSupplier(final Supplier<ThreadContextMap> threadContextMapSupplier) {
- this.threadContextMapSupplier = threadContextMapSupplier;
+ public void setThreadContextMapFactory(final Supplier<ThreadContextMap> threadContextMapFactory) {
+ threadContextMapFactoryLazy.set(threadContextMapFactory);
}
- public void setThreadContextStackSupplier(final Supplier<ThreadContextStack> threadContextStackSupplier) {
- this.threadContextStackSupplier = threadContextStackSupplier;
+ public void setThreadContextStackFactory(final Supplier<ThreadContextStack> threadContextStackFactory) {
+ threadContextStackFactoryLazy.set(threadContextStackFactory);
}
/**
@@ -199,14 +202,14 @@ public class LoggingSystem {
* Creates a new ThreadContextMap.
*/
public static ThreadContextMap createContextMap() {
- return getInstance().threadContextMapSupplier.get();
+ return getInstance().threadContextMapFactoryLazy.value().get();
}
/**
* Creates a new ThreadContextStack.
*/
public static ThreadContextStack createContextStack() {
- return getInstance().threadContextStackSupplier.get();
+ return getInstance().threadContextStackFactoryLazy.value().get();
}
private static List<Provider> loadDefaultProviders() {
@@ -295,8 +298,7 @@ public class LoggingSystem {
this.provider = provider;
}
- public LoggerContextFactory createLoggerContextFactory() {
- final PropertyEnvironment environment = PropertiesUtil.getProperties();
+ public LoggerContextFactory createLoggerContextFactory(final PropertyEnvironment environment) {
final String customFactoryClass = environment.getStringProperty(LogManager.FACTORY_PROPERTY_NAME);
if (customFactoryClass != null) {
final LoggerContextFactory customFactory = createInstance(customFactoryClass, LoggerContextFactory.class);
@@ -339,8 +341,7 @@ public class LoggingSystem {
* @see ReadOnlyThreadContextMap
* @see org.apache.logging.log4j.ThreadContext
*/
- public ThreadContextMap createContextMap() {
- final PropertyEnvironment environment = PropertiesUtil.getProperties();
+ public ThreadContextMap createContextMap(final PropertyEnvironment environment) {
final String customThreadContextMap = environment.getStringProperty(THREAD_CONTEXT_MAP_CLASS);
if (customThreadContextMap != null) {
final ThreadContextMap customContextMap = createInstance(customThreadContextMap, ThreadContextMap.class);
@@ -374,8 +375,7 @@ public class LoggingSystem {
return new DefaultThreadContextMap(true, inheritableMap);
}
- public ThreadContextStack createContextStack() {
- final PropertyEnvironment environment = PropertiesUtil.getProperties();
+ public ThreadContextStack createContextStack(final PropertyEnvironment environment) {
final boolean disableStack = environment.getBooleanProperty(THREAD_CONTEXT_STACK_DISABLED,
environment.getBooleanProperty(THREAD_CONTEXT_DISABLED));
return new DefaultThreadContextStack(!disableStack);