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 2023/01/02 07:05:56 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-2297 - Allow java.sql.Time objects to format properly in MapMessages.

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 ff2abf866d LOG4J2-2297 - Allow java.sql.Time objects to format properly in MapMessages.
ff2abf866d is described below

commit ff2abf866d559cc7d75a109e920a5f58236b2c0f
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Mon Jan 2 00:05:36 2023 -0700

    LOG4J2-2297 - Allow java.sql.Time objects to format properly in MapMessages.
---
 log4j-api-java9/src/main/java/module-info.java     |  1 +
 .../logging/log4j/message/ParameterFormatter.java  | 11 ++++-------
 .../apache/logging/log4j/util/StringBuilders.java  | 23 +++++++++++++++++++++-
 src/changes/changes.xml                            |  3 +++
 4 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/log4j-api-java9/src/main/java/module-info.java b/log4j-api-java9/src/main/java/module-info.java
index 0008f537d0..900f9d7988 100644
--- a/log4j-api-java9/src/main/java/module-info.java
+++ b/log4j-api-java9/src/main/java/module-info.java
@@ -20,6 +20,7 @@ import org.apache.logging.log4j.util.SystemPropertiesPropertySource;
 
 module org.apache.logging.log4j {
     requires java.base;
+    requires static java.sql;
 
     exports org.apache.logging.log4j;
     exports org.apache.logging.log4j.message;
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java
index c0994ada32..7e3b3a86ca 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java
@@ -16,11 +16,7 @@
  */
 package org.apache.logging.log4j.message;
 
-import java.sql.Time;
-import java.text.SimpleDateFormat;
-import java.time.LocalDateTime;
 import java.time.ZoneId;
-import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Arrays;
 import java.util.Collection;
@@ -66,7 +62,8 @@ final class ParameterFormatter {
     private static final char DELIM_STOP = '}';
     private static final char ESCAPE_CHAR = '\\';
 
-    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
+            .withZone(ZoneId.systemDefault());
 
     private ParameterFormatter() {
     }
@@ -489,10 +486,10 @@ final class ParameterFormatter {
     }
 
     private static boolean appendDate(final Object o, final StringBuilder str) {
-        if (o instanceof Time || !(o instanceof Date)) {
+        if (!(o instanceof Date)) {
             return false;
         }
-        str.append(ZonedDateTime.ofInstant(((Date) o).toInstant(), ZoneId.systemDefault()).format(DATE_FORMATTER));
+        str.append(DATE_FORMATTER.format(((Date) o).toInstant()));
         return true;
     }
 
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 3868850e85..7b4b9bf731 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
@@ -24,6 +24,19 @@ import static java.lang.Character.toLowerCase;
  * <em>Consider this class private.</em>
  */
 public final class StringBuilders {
+
+    private static final Class<?> timeClass;
+
+    static {
+        Class<?> clazz;
+        try {
+            clazz = Class.forName("java.sql.Time");
+        } catch(ClassNotFoundException ex) {
+            clazz = null;
+        }
+        timeClass = clazz;
+    }
+
     private StringBuilders() {
     }
 
@@ -97,7 +110,7 @@ public final class StringBuilders {
             stringBuilder.append(((Float) obj).floatValue());
         } else if (obj instanceof Byte) {
             stringBuilder.append(((Byte) obj).byteValue());
-        } else if (obj instanceof java.sql.Time || obj instanceof java.time.temporal.Temporal) {
+        } else if (isTime(obj) || obj instanceof java.time.temporal.Temporal) {
             stringBuilder.append(obj);
         } else {
             return false;
@@ -105,6 +118,14 @@ public final class StringBuilders {
         return true;
     }
 
+    /*
+        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());
+    }
+
+
     /**
      * Returns true if the specified section of the left CharSequence equals the specified section of the right
      * CharSequence.
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 94c3022968..48665a81b4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
          - "remove" - Removed
     -->
     <release version="2.19.1" date="TBD" description="GA Release 2.19.1">
+      <action issue="LOG4J2-2297" dev="rgoers" type="fix">
+        Allow java.sql.Time objects to format properly in MapMessages.
+      </action>
       <action issue="LOG4J2-2785" dev="rgoers" type="add" due-to="Markus Spann">
         Pattern Layout to abbreviate the name of all logger components except the 2 rightmost.
       </action>