Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2297

java.sql.Time objects are incorrectly formatted

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.11.0
    • 2.20.0
    • None
    • None

    Description

      Log4j2 incorrectly formats instance objects of java.sql.Time. Consider following case -

      final Time time = Time.valueOf("12:00:00");
      logger.info("Time : {}", time);

       
      Now, the expected output is Time : 12:00:00 but instead it is logged as Time : 1970-01-01T12:00:00.000+0530. Since java.sql.Time has no date component, it is wrong to format it this way.
       

      Attachments

        Activity

          rgoers Ralph Goers added a comment -

          The problem here is pretty obvious. ParameterFormatter's appendSpecialTypes method calls StringBuilders.appendSpecificTypes method, which does not handle dates or times (I wonder why) and then calls appendDate. Since java.sql.Time extends java.util.Date the formatter there is used. This code does not handle any of the classes from Java 8's java.time package.

          In my opinion appendSpecificTypes should be modified to handle all the various dates and times, including java.time. The Log4j 2 3.x version should use java.time's formatter to format them.

          rgoers Ralph Goers added a comment - The problem here is pretty obvious. ParameterFormatter's appendSpecialTypes method calls StringBuilders.appendSpecificTypes method, which does not handle dates or times (I wonder why) and then calls appendDate. Since java.sql.Time extends java.util.Date the formatter there is used. This code does not handle any of the classes from Java 8's java.time package. In my opinion appendSpecificTypes should be modified to handle all the various dates and times, including java.time. The Log4j 2 3.x version should use java.time's formatter to format them.
          rgoers Ralph Goers added a comment -

          java.sql.Time and all the java.time objects have toString methods that already format correctly, so the change here is to skip formatting the Time in appendDate.

          rgoers Ralph Goers added a comment - java.sql.Time and all the java.time objects have toString methods that already format correctly, so the change here is to skip formatting the Time in appendDate.
          rgoers Ralph Goers added a comment -

          Patch applied.

          rgoers Ralph Goers added a comment - Patch applied.
          rturner@e-djuster.com Robert Turner added a comment - - edited

          I'm seeing a crash in 2.20.0 now that I think is related to this change – when passing a `java.sql.Date` object as a parameter, an `UnsupportedOperationException` is now thrown as `toInstant` is not support on them (https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html#toInstant–)

          Top of backtrace:

          Caused by: java.lang.UnsupportedOperationException
                  at java.sql/java.sql.Date.toInstant(Date.java:316)
                  at org.apache.logging.log4j.message.ParameterFormatter.appendDate(ParameterFormatter.java:492)
                  at org.apache.logging.log4j.message.ParameterFormatter.appendSpecialTypes(ParameterFormatter.java:485)
                  at org.apache.logging.log4j.message.ParameterFormatter.recursiveDeepToString(ParameterFormatter.java:474)
                  at org.apache.logging.log4j.message.ParameterFormatter.recursiveDeepToString(ParameterFormatter.java:449)
                  at org.apache.logging.log4j.message.ParameterFormatter.formatMessage2(ParameterFormatter.java:192)
                  at org.apache.logging.log4j.message.ParameterizedMessage.formatTo(ParameterizedMessage.java:227)
          

          I have created https://github.com/apache/logging-log4j2/issues/1400 for this issue.

          rturner@e-djuster.com Robert Turner added a comment - - edited I'm seeing a crash in 2.20.0 now that I think is related to this change – when passing a `java.sql.Date` object as a parameter, an `UnsupportedOperationException` is now thrown as `toInstant` is not support on them ( https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html#toInstant –) Top of backtrace: Caused by: java.lang.UnsupportedOperationException         at java.sql/java.sql.Date.toInstant(Date.java:316)         at org.apache.logging.log4j.message.ParameterFormatter.appendDate(ParameterFormatter.java:492)         at org.apache.logging.log4j.message.ParameterFormatter.appendSpecialTypes(ParameterFormatter.java:485)         at org.apache.logging.log4j.message.ParameterFormatter.recursiveDeepToString(ParameterFormatter.java:474)         at org.apache.logging.log4j.message.ParameterFormatter.recursiveDeepToString(ParameterFormatter.java:449)         at org.apache.logging.log4j.message.ParameterFormatter.formatMessage2(ParameterFormatter.java:192)         at org.apache.logging.log4j.message.ParameterizedMessage.formatTo(ParameterizedMessage.java:227) I have created https://github.com/apache/logging-log4j2/issues/1400 for this issue.

          People

            rgoers Ralph Goers
            kedarjoshi Kedar Joshi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: