From 1a0e76578876c56388549da9f1df2e22b1d6034e Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Tue, 3 Sep 2024 21:48:24 +0200 Subject: [PATCH] minor changes in DateConverter Signed-off-by: Ceki Gulcu --- .../qos/logback/classic/pattern/DateConverter.java | 11 +++-------- .../qos/logback/classic/pattern/ConverterTest.java | 14 +++++++------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/pattern/DateConverter.java b/logback-classic/src/main/java/ch/qos/logback/classic/pattern/DateConverter.java index e05c570e6b..33b337178f 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/pattern/DateConverter.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/pattern/DateConverter.java @@ -23,8 +23,6 @@ public class DateConverter extends ClassicConverter { - long lastTimestamp = -1; - String timestampStrCache = null; CachingDateFormatter cachingDateFormatter = null; public void start() { @@ -33,13 +31,9 @@ public void start() { if (datePattern == null) { datePattern = CoreConstants.ISO8601_PATTERN; - } - - if (datePattern.equals(CoreConstants.ISO8601_STR)) { + } else if (datePattern.equals(CoreConstants.ISO8601_STR)) { datePattern = CoreConstants.ISO8601_PATTERN; - } - - if (datePattern.equals(CoreConstants.STRICT_STR)) { + } else if (datePattern.equals(CoreConstants.STRICT_STR)) { datePattern = CoreConstants.STRICT_ISO8601_PATTERN; } @@ -60,6 +54,7 @@ public void start() { } try { // if zoneId is null, the CachingDateFormatter will use the ZoneId.systemDefault() + // if locale is null, the CachingDateFormatter will use the Locale.getDefault() cachingDateFormatter = new CachingDateFormatter(datePattern, zoneId, locale); } catch (IllegalArgumentException e) { addWarn("Could not instantiate SimpleDateFormat with pattern " + datePattern, e); diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java index 73dccc71a3..aa498508e5 100644 --- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java +++ b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java @@ -394,14 +394,16 @@ public void testSequenceNumber() { @Test void dateConverterTest() { - dateConverterChecker(List.of("STRICT", "GMT"), "2024-08-14T15:29:25,956"); - dateConverterChecker(List.of("ISO8601", "GMT"), "2024-08-14 15:29:25,956"); - dateConverterChecker(List.of("ISO8601", "UTC"), "2024-08-14 15:29:25,956"); - dateConverterChecker(List.of("yyyy-MM-EE", "UTC", "fr-CH"), "2024-08-mer."); + // 2024-08-14T1Z:29:25,956 GMT + long millis = 1_723_649_365_956L; + dateConverterChecker(millis, List.of("STRICT", "GMT"), "2024-08-14T15:29:25,956"); + dateConverterChecker(millis, List.of("ISO8601", "GMT"), "2024-08-14 15:29:25,956"); + dateConverterChecker(millis, List.of("ISO8601", "UTC"), "2024-08-14 15:29:25,956"); + dateConverterChecker(millis, List.of("yyyy-MM-EE", "UTC", "fr-CH"), "2024-08-mer."); } - void dateConverterChecker(List options, String expected) { + void dateConverterChecker(long millis, List options, String expected) { DateConverter dateConverter = new DateConverter(); dateConverter.setOptionList(options) ; dateConverter.setContext(loggerContext); @@ -410,8 +412,6 @@ void dateConverterChecker(List options, String expected) { assertTrue(dateConverter.isStarted()); LoggingEvent event = makeLoggingEvent(null); - // 2024-08-14T1Z:29:25,956 GMT - long millis = 1_723_649_365_956L; //System.currentTimeMillis(); Instant now = Instant.ofEpochMilli(millis); event.setInstant(now); String result = dateConverter.convert(event);