[LOG4J2-2732] Add ThreadContext.putIfNull(key, value) helper method
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..3baac53 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,23 @@
}
/**
+ * 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.
+ */
+ public static void putIfNull(final String key, final String value) {
+ if(!contextMap.contains(key)) {
+ contextMap.put(key, value);
+ }
+ }
+
+ /**
* Puts all given context map entries into the current thread's
* context map.
*