This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 3e551ac9f0b44a54e15676d55169b697537e8141
Author: Matt Sicker <[email protected]>
AuthorDate: Sat Mar 18 14:43:33 2023 -0500

    Update Key to implement StringBuilderFormattable
    
    Signed-off-by: Matt Sicker <[email protected]>
---
 .../org/apache/logging/log4j/plugins/di/Key.java   | 28 +++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git 
a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Key.java 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Key.java
index e62d0f1c5f..ac5983129d 100644
--- a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Key.java
+++ b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/Key.java
@@ -33,6 +33,7 @@ import org.apache.logging.log4j.plugins.QualifierType;
 import org.apache.logging.log4j.plugins.util.AnnotationUtil;
 import org.apache.logging.log4j.plugins.util.TypeUtil;
 import org.apache.logging.log4j.util.Cast;
+import org.apache.logging.log4j.util.StringBuilderFormattable;
 import org.apache.logging.log4j.util.Strings;
 
 /**
@@ -41,7 +42,7 @@ import org.apache.logging.log4j.util.Strings;
  *
  * @param <T> type of key
  */
-public class Key<T> {
+public class Key<T> implements StringBuilderFormattable {
     private final Type type;
     private final Class<T> rawType;
     private final Class<? extends Annotation> qualifierType;
@@ -192,12 +193,33 @@ public class Key<T> {
     public final String toString() {
         String string = toString;
         if (string == null) {
-            toString = string = String.format("Key{namespace='%s', name='%s', 
type=%s, qualifierType=%s}",
-                    namespace, name, type.getTypeName(), qualifierType != null 
? qualifierType.getSimpleName() : Strings.EMPTY);
+            StringBuilder sb = new StringBuilder(32);
+            formatTo(sb);
+            toString = string = sb.toString();
         }
         return string;
     }
 
+    @Override
+    public void formatTo(final StringBuilder buffer) {
+        buffer.append(TO_STRING_PREFIX).append(type.getTypeName());
+        if (!namespace.isEmpty()) {
+            buffer.append(NAMESPACE).append(namespace);
+        }
+        if (!name.isEmpty()) {
+            buffer.append(NAME).append(name);
+        }
+        if (qualifierType != null) {
+            
buffer.append(QUALIFIER_TYPE).append(qualifierType.getSimpleName());
+        }
+        buffer.append(']');
+    }
+
+    private static final String TO_STRING_PREFIX = "Key[type: ";
+    private static final String NAMESPACE = "; namespace: ";
+    private static final String NAME = "; name: ";
+    private static final String QUALIFIER_TYPE = "; qualifierType: ";
+
     /**
      * Creates a Key for the class.
      */

Reply via email to