You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2019/12/07 04:21:42 UTC
[logging-log4j2] branch release-2.x updated: LOG4J2-2732 - Add
ThreadContext.putIfNotNull method
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new f7e3b15 LOG4J2-2732 - Add ThreadContext.putIfNotNull method
f7e3b15 is described below
commit f7e3b15ace9cc1fdc0c007489125730043dd1b57
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Fri Dec 6 21:21:26 2019 -0700
LOG4J2-2732 - Add ThreadContext.putIfNotNull method
---
.../java/org/apache/logging/log4j/ThreadContext.java | 18 ++++++++++++++++++
.../org/apache/logging/log4j/ThreadContextTest.java | 12 ++++++++++++
src/changes/changes.xml | 3 +++
3 files changed, 33 insertions(+)
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
index c4ae445..5318410 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/ThreadContext.java
@@ -247,6 +247,24 @@ public final class ThreadContext {
}
/**
+ * Puts a context value (the <code>value</code> parameter) as identified with the <code>key</code> parameter into
+ * the current thread's context map if the key does not exist.
+ *
+ * <p>
+ * If the current thread does not have a context map it is created as a side effect.
+ * </p>
+ *
+ * @param key The key name.
+ * @param value The key value.
+ * @since 2.13.0
+ */
+ public static void putIfNull(final String key, final String value) {
+ if(!contextMap.containsKey(key)) {
+ contextMap.put(key, value);
+ }
+ }
+
+ /**
* Puts all given context map entries into the current thread's
* context map.
*
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
index baec493..63f21df 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextTest.java
@@ -108,6 +108,18 @@ public class ThreadContextTest {
}
@Test
+ public void testPutIfNotNull() {
+ ThreadContext.clearMap();
+ assertNull(ThreadContext.get("testKey"));
+ ThreadContext.put("testKey", "testValue");
+ assertEquals("testValue", ThreadContext.get("testKey"));
+ assertEquals("Incorrect value in test key", "testValue", ThreadContext.get("testKey"));
+ ThreadContext.putIfNull("testKey", "new Value");
+ assertEquals("Incorrect value in test key", "testValue", ThreadContext.get("testKey"));
+ ThreadContext.clearMap();
+ }
+
+ @Test
public void testPutAll() {
assertTrue(ThreadContext.isEmpty());
assertFalse(ThreadContext.containsKey("key"));
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 733d5a9..f29a418 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
- "remove" - Removed
-->
<release version="2.13.0" date="2019-MM-DD" description="GA Release 2.13.0">
+ <action issue="LOG4J2-2732" dev="rgoers" type="add" due-to="Matt Pavlovich">
+ Add ThreadContext.putIfNotNull method.
+ </action>
<action issue="LOG4J2-2731" dev="rgoers" type="add">
Add a Level Patttern Selector.
</action>