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 2023/01/28 22:50:38 UTC

[logging-log4j2] branch master updated: Improve consistency of ThreadContext API behavior

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 3134b2d5db Improve consistency of ThreadContext API behavior
3134b2d5db is described below

commit 3134b2d5dbf794cda9c6f931d6fdbd5bb1c65d59
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Sat Jan 28 16:50:26 2023 -0600

    Improve consistency of ThreadContext API behavior
    
    Signed-off-by: Matt Sicker <ma...@apache.org>
---
 .../org/apache/logging/log4j/ThreadContext.java     | 21 +++++++++++++++------
 .../log4j/spi/DefaultThreadContextStack.java        |  2 ++
 .../log4j/spi/MutableThreadContextStack.java        |  6 ++++--
 3 files changed, 21 insertions(+), 8 deletions(-)

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 ccaa6eb5db..7ff1c89b95 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
@@ -22,6 +22,7 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.logging.log4j.message.ParameterizedMessage;
 import org.apache.logging.log4j.spi.DefaultThreadContextMap;
@@ -31,6 +32,7 @@ import org.apache.logging.log4j.spi.ReadOnlyThreadContextMap;
 import org.apache.logging.log4j.spi.ThreadContextMap;
 import org.apache.logging.log4j.spi.ThreadContextStack;
 import org.apache.logging.log4j.util.InternalApi;
+import org.apache.logging.log4j.util.Strings;
 
 /**
  * The ThreadContext allows applications to store information either in a Map or a Stack.
@@ -50,12 +52,12 @@ public final class ThreadContext {
 
         @Override
         public String pop() {
-            return null;
+            return Strings.EMPTY;
         }
 
         @Override
         public String peek() {
-            return null;
+            return Strings.EMPTY;
         }
 
         @Override
@@ -364,8 +366,10 @@ public final class ThreadContext {
      * Sets this thread's stack.
      *
      * @param stack The stack to use.
+     * @throws NullPointerException if stack is null
      */
     public static void setStack(final Collection<String> stack) {
+        Objects.requireNonNull(stack, "No stack provided");
         if (stack.isEmpty()) {
             return;
         }
@@ -420,6 +424,7 @@ public final class ThreadContext {
      * </p>
      *
      * @param message The new diagnostic context information.
+     * @throws UnsupportedOperationException if the context stack is disabled
      */
     public static void push(final String message) {
         contextStack.push(message);
@@ -436,6 +441,7 @@ public final class ThreadContext {
      *
      * @param message The new diagnostic context information.
      * @param args Parameters for the message.
+     * @throws UnsupportedOperationException if the context stack is disabled
      */
     public static void push(final String message, final Object... args) {
         contextStack.push(ParameterizedMessage.format(message, args));
@@ -492,6 +498,7 @@ public final class ThreadContext {
      *
      * @see #getDepth
      * @param depth The number of elements to keep.
+     * @throws IllegalArgumentException if depth is negative
      */
     public static void trim(final int depth) {
         contextStack.trim(depth);
@@ -503,17 +510,16 @@ public final class ThreadContext {
     public interface ContextStack extends Collection<String> {
 
         /**
-         * Returns the element at the top of the stack.
+         * Returns the element at the top of the stack. If the stack is empty, then the empty string is returned.
          *
          * @return The element at the top of the stack.
-         * @throws java.util.NoSuchElementException if the stack is empty.
          */
         String pop();
 
         /**
-         * Returns the element at the top of the stack without removing it or null if the stack is empty.
+         * Returns the element at the top of the stack without removing it or the empty string if the stack is empty.
          *
-         * @return the element at the top of the stack or null if the stack is empty.
+         * @return the element at the top of the stack or the empty string if the stack is empty.
          */
         String peek();
 
@@ -521,6 +527,8 @@ public final class ThreadContext {
          * Pushes an element onto the stack.
          *
          * @param message The element to add.
+         * @throws NullPointerException if message is null
+         * @throws UnsupportedOperationException if the context stack is disabled
          */
         void push(String message);
 
@@ -542,6 +550,7 @@ public final class ThreadContext {
          * Trims elements from the end of the stack.
          *
          * @param depth The maximum number of items in the stack to keep.
+         * @throws IllegalArgumentException if depth is negative
          */
         void trim(int depth);
 
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextStack.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextStack.java
index a16c8a97e1..25f6e78b0e 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextStack.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/DefaultThreadContextStack.java
@@ -20,6 +20,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 
 import org.apache.logging.log4j.ThreadContext.ContextStack;
 import org.apache.logging.log4j.util.StringBuilderFormattable;
@@ -194,6 +195,7 @@ public class DefaultThreadContextStack implements ThreadContextStack, StringBuil
 
     @Override
     public void push(final String message) {
+        Objects.requireNonNull(message, "No message provided");
         if (!useStack) {
             return;
         }
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/MutableThreadContextStack.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/MutableThreadContextStack.java
index 959ed3507c..dcce4a72ca 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/MutableThreadContextStack.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/MutableThreadContextStack.java
@@ -24,6 +24,7 @@ import java.util.Objects;
 
 import org.apache.logging.log4j.ThreadContext.ContextStack;
 import org.apache.logging.log4j.util.StringBuilderFormattable;
+import org.apache.logging.log4j.util.Strings;
 
 /**
  * TODO
@@ -65,7 +66,7 @@ public class MutableThreadContextStack implements ThreadContextStack, StringBuil
     public String pop() {
         checkInvariants();
         if (list.isEmpty()) {
-            return null;
+            return Strings.EMPTY;
         }
         final int last = list.size() - 1;
         return list.remove(last);
@@ -74,7 +75,7 @@ public class MutableThreadContextStack implements ThreadContextStack, StringBuil
     @Override
     public String peek() {
         if (list.isEmpty()) {
-            return null;
+            return Strings.EMPTY;
         }
         final int last = list.size() - 1;
         return list.get(last);
@@ -82,6 +83,7 @@ public class MutableThreadContextStack implements ThreadContextStack, StringBuil
 
     @Override
     public void push(final String message) {
+        Objects.requireNonNull(message, "No message provided");
         checkInvariants();
         list.add(message);
     }