ppkarwasz commented on code in PR #2374: URL: https://github.com/apache/logging-log4j2/pull/2374#discussion_r1527118053
########## log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextMap.java: ########## @@ -43,40 +43,30 @@ public class DefaultThreadContextMap implements ThreadContextMap, ReadOnlyString private final boolean useMap; private final ThreadLocal<Map<String, String>> localMap; - private static boolean inheritableMap; - - static { - init(); - } - - // LOG4J2-479: by default, use a plain ThreadLocal, only use InheritableThreadLocal if configured. - // (This method is package protected for JUnit tests.) - static ThreadLocal<Map<String, String>> createThreadLocalMap(final boolean isMapEnabled) { - if (inheritableMap) { - return new InheritableThreadLocal<Map<String, String>>() { - @Override - protected Map<String, String> childValue(final Map<String, String> parentValue) { - return parentValue != null && isMapEnabled // - ? Collections.unmodifiableMap(new HashMap<>(parentValue)) // - : null; - } - }; - } - // if not inheritable, return plain ThreadLocal with null as initial value - return new ThreadLocal<>(); - } - - static void init() { - inheritableMap = PropertiesUtil.getProperties().getBooleanProperty(INHERITABLE_MAP); - } - public DefaultThreadContextMap() { this(true); } + /** + * @deprecated Since 2.24.0 use the default constructor or {@link NoOpThreadContextMap} instead. + */ + @Deprecated public DefaultThreadContextMap(final boolean useMap) { + this(useMap, PropertiesUtil.getProperties()); + } + + DefaultThreadContextMap(final boolean useMap, final PropertiesUtil properties) { this.useMap = useMap; - this.localMap = createThreadLocalMap(useMap); + localMap = properties.getBooleanProperty(INHERITABLE_MAP) + ? new InheritableThreadLocal<Map<String, String>>() { + @Override + protected Map<String, String> childValue(final Map<String, String> parentValue) { + return parentValue != null && useMap + ? Collections.unmodifiableMap(new HashMap<>(parentValue)) + : null; + } + } + : new ThreadLocal<Map<String, String>>(); Review Comment: `localMap` must be a `ThreadLocal`, ;-) If `!useMap` I could probably set `localMap` to `null`, but our code (see `Provider`) does not use `DefaultThreadContextMap` in such a case. I leave the `useMap` parameter only for backward compatibility. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org