-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Move ThreadContextMap
implementations to log4j-core
#2593
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f43b42d
Move `ThreadContextMap` implementations to `log4j-core`
ppkarwasz 2b87b8d
Add inheritability to `StringArrayThreadContextMap`
ppkarwasz 49478b9
Fix `log4j-to-jul` and `log4j-to-slf4j` providers
ppkarwasz b83699f
Fix `OpenHashStringMap` inheritance
ppkarwasz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 0 additions & 39 deletions
39
...-api-test/src/main/java/org/apache/logging/log4j/test/junit/InitializesThreadContext.java
This file was deleted.
Oops, something went wrong.
58 changes: 0 additions & 58 deletions
58
...-api-test/src/main/java/org/apache/logging/log4j/test/junit/ThreadContextInitializer.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
...pi-test/src/main/java/org/apache/logging/log4j/test/spi/AbstractThreadContextMapTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.logging.log4j.test.spi; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import org.apache.logging.log4j.spi.ThreadContextMap; | ||
|
||
/** | ||
* Provides a set of utility methods to test implementations of {@link ThreadContextMap}. | ||
*/ | ||
public abstract class AbstractThreadContextMapTest { | ||
|
||
private static final String KEY = "key"; | ||
|
||
/** | ||
* Implementations SHOULD not propagate the context to newly created threads by default. | ||
* | ||
* @param contextMap A {@link ThreadContextMap implementation}. | ||
*/ | ||
protected static void assertThreadLocalNotInheritable(final ThreadContextMap contextMap) { | ||
contextMap.put(KEY, "threadLocalNotInheritableByDefault"); | ||
verifyThreadContextValueFromANewThread(contextMap, null); | ||
} | ||
|
||
/** | ||
* Implementations MAY offer a configuration that propagates the context to newly created threads. | ||
* | ||
* @param contextMap A {@link ThreadContextMap implementation}. | ||
*/ | ||
protected static void assertThreadLocalInheritable(final ThreadContextMap contextMap) { | ||
contextMap.put(KEY, "threadLocalInheritableIfConfigured"); | ||
verifyThreadContextValueFromANewThread(contextMap, "threadLocalInheritableIfConfigured"); | ||
} | ||
|
||
private static void verifyThreadContextValueFromANewThread( | ||
final ThreadContextMap contextMap, final String expected) { | ||
final ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
try { | ||
assertThat(executorService.submit(() -> contextMap.get(KEY))) | ||
.succeedsWithin(Duration.ofSeconds(1)) | ||
.isEqualTo(expected); | ||
} finally { | ||
executorService.shutdown(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,12 @@ | |
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.logging.log4j.internal.map.StringArrayThreadContextMap; | ||
import org.apache.logging.log4j.message.ParameterizedMessage; | ||
import org.apache.logging.log4j.spi.CleanableThreadContextMap; | ||
import org.apache.logging.log4j.spi.DefaultThreadContextMap; | ||
import org.apache.logging.log4j.spi.DefaultThreadContextStack; | ||
import org.apache.logging.log4j.spi.MutableThreadContextStack; | ||
import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap; | ||
import org.apache.logging.log4j.spi.ThreadContextMap; | ||
import org.apache.logging.log4j.spi.ThreadContextMap2; | ||
import org.apache.logging.log4j.spi.ThreadContextMapFactory; | ||
import org.apache.logging.log4j.spi.ThreadContextStack; | ||
import org.apache.logging.log4j.util.PropertiesUtil; | ||
|
@@ -268,21 +265,11 @@ public static void putIfNull(final String key, final String value) { | |
* | ||
* <p>If the current thread does not have a context map it is | ||
* created as a side effect.</p> | ||
* @param m The map. | ||
* @param map The map. | ||
* @since 2.7 | ||
*/ | ||
public static void putAll(final Map<String, String> m) { | ||
if (contextMap instanceof ThreadContextMap2) { | ||
((ThreadContextMap2) contextMap).putAll(m); | ||
} else if (contextMap instanceof DefaultThreadContextMap) { | ||
((DefaultThreadContextMap) contextMap).putAll(m); | ||
} else if (contextMap instanceof StringArrayThreadContextMap) { | ||
((StringArrayThreadContextMap) contextMap).putAll(m); | ||
} else { | ||
for (final Map.Entry<String, String> entry : m.entrySet()) { | ||
contextMap.put(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
public static void putAll(final Map<String, String> map) { | ||
contextMap.putAll(map); | ||
} | ||
|
||
/** | ||
|
@@ -316,17 +303,7 @@ public static void remove(final String key) { | |
* @since 2.8 | ||
*/ | ||
public static void removeAll(final Iterable<String> keys) { | ||
if (contextMap instanceof CleanableThreadContextMap) { | ||
((CleanableThreadContextMap) contextMap).removeAll(keys); | ||
} else if (contextMap instanceof DefaultThreadContextMap) { | ||
((DefaultThreadContextMap) contextMap).removeAll(keys); | ||
} else if (contextMap instanceof StringArrayThreadContextMap) { | ||
((StringArrayThreadContextMap) contextMap).removeAll(keys); | ||
} else { | ||
for (final String key : keys) { | ||
contextMap.remove(key); | ||
} | ||
} | ||
contextMap.removeAll(keys); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please maintain separate if/else branches for the most common instance types, as in the previous version. This avoids virtual method lookups for those types. |
||
} | ||
|
||
/** | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please maintain separate if/else branches for the most common instance types, as in the previous version. This avoids virtual method lookups for those types.