From dc047411a48c1daa1b25a6f0e6c3430e0cadf0d9 Mon Sep 17 00:00:00 2001 From: Lucas Souza Date: Fri, 17 Mar 2023 23:49:11 -0300 Subject: [PATCH] #1366 - Fixed logging of java.sql.Date --- .../logging/log4j/util/StringBuilders.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java index 7b4b9bf731c..0374c79afbb 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/StringBuilders.java @@ -26,6 +26,7 @@ public final class StringBuilders { private static final Class timeClass; + private static final Class dateClass; static { Class clazz; @@ -35,6 +36,13 @@ public final class StringBuilders { clazz = null; } timeClass = clazz; + + try { + clazz = Class.forName("java.sql.Date"); + } catch(ClassNotFoundException ex) { + clazz = null; + } + dateClass = clazz; } private StringBuilders() { @@ -110,7 +118,7 @@ public static boolean appendSpecificTypes(final StringBuilder stringBuilder, fin stringBuilder.append(((Float) obj).floatValue()); } else if (obj instanceof Byte) { stringBuilder.append(((Byte) obj).byteValue()); - } else if (isTime(obj) || obj instanceof java.time.temporal.Temporal) { + } else if (isTime(obj) || isDate(obj) || obj instanceof java.time.temporal.Temporal) { stringBuilder.append(obj); } else { return false; @@ -119,12 +127,18 @@ public static boolean appendSpecificTypes(final StringBuilder stringBuilder, fin } /* - Check to see if obj is an instance of java.sql.time without requiring the java.sql module. + Check to see if obj is an instance of java.sql.Time without requiring the java.sql module. */ private static boolean isTime(final Object obj) { return timeClass != null && timeClass.isAssignableFrom(obj.getClass()); } + /* + Check to see if obj is an instance of java.sql.Date without requiring the java.sql module. + */ + private static boolean isDate(final Object obj) { + return dateClass != null && dateClass.isAssignableFrom(obj.getClass()); + } /** * Returns true if the specified section of the left CharSequence equals the specified section of the right