From edfd89a11d1c5198d1b49d8ba32e262274e2b935 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Thu, 28 Jan 2021 09:18:39 +0100 Subject: [PATCH] Fixes for timestamp parsing. --- .../syslogd/parser/SyslogParserRfc3164.java | 10 +++---- .../syslogd/parser/SyslogParserRfc5424.java | 28 +++++++++---------- .../syslogd/SyslogParserRfc5424Test.groovy | 14 +++++----- src/test/resources/simplelogger.properties | 2 +- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc3164.java b/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc3164.java index d6643e0..01e0347 100644 --- a/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc3164.java +++ b/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc3164.java @@ -86,18 +86,18 @@ public class SyslogParserRfc3164 extends SyslogParser { */ public Instant parseTimestamp(String dateString) { - // We need to add year to parse date correctly + // We need to add current year to parse date correctly OffsetDateTime odt = OffsetDateTime.now(); // Date: Mmm dd hh:mm:ss - Instant instant = null; try { - instant = Instant.from(dateTimeFormatter.parse(odt.getYear() + " " + dateString)); + return Instant.from(dateTimeFormatter.parse(odt.getYear() + " " + dateString)); } catch(DateTimeParseException e) { - log.error("parseDate()", e); + log.debug("parseTimestamp()", e); } - return instant; + log.warn("parseTimestamp() - Could not parse timestamp: " + dateString); + return Instant.now(); } } diff --git a/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc5424.java b/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc5424.java index 628e373..aed8108 100644 --- a/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc5424.java +++ b/src/main/java/biz/nellemann/syslogd/parser/SyslogParserRfc5424.java @@ -21,9 +21,11 @@ import biz.nellemann.syslogd.msg.SyslogMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; +import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; @@ -106,11 +108,11 @@ public class SyslogParserRfc5424 extends SyslogParser { */ List formatStrings = Arrays.asList( - "yyyy-MM-dd'T'HH:mm:ss.SS'Z'", - "yyyy-MM-dd'T'HH:mm:ss.SSXXX", - "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", - "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX", - "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX" + //"yyyy-MM-dd'T'HH:mm:ss.SS'X'", + "yyyy-MM-dd'T'HH:mm:ss.SSX", + "yyyy-MM-dd'T'HH:mm:ss.SSSX", + "yyyy-MM-dd'T'HH:mm:ss.SSSSSSX", + "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX" ); for(String formatString : formatStrings) @@ -118,17 +120,13 @@ public class SyslogParserRfc5424 extends SyslogParser { try { return new SimpleDateFormat(formatString).parse(dateString).toInstant(); } - catch (ParseException e) {} + catch (ParseException e) { + log.debug("parseTimestamp()", e); + } } - /* - try { - DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; - instant = Instant.from(dateTimeFormatter.parse(dateString)); - } catch(DateTimeParseException e) { - log.error("parseTimestamp()", e); - } - return instant;*/ - return null; + + log.warn("parseTimestamp() - Could not parse timestamp: " + dateString); + return Instant.now(); } } diff --git a/src/test/groovy/biz/nellemann/syslogd/SyslogParserRfc5424Test.groovy b/src/test/groovy/biz/nellemann/syslogd/SyslogParserRfc5424Test.groovy index 5e6dce8..624c6df 100644 --- a/src/test/groovy/biz/nellemann/syslogd/SyslogParserRfc5424Test.groovy +++ b/src/test/groovy/biz/nellemann/syslogd/SyslogParserRfc5424Test.groovy @@ -67,8 +67,8 @@ class SyslogParserRfc5424Test extends Specification { Instant inst = syslogParser.parseTimestamp(dateString) then: - inst.toEpochMilli() == 482188850052 - //inst.toString() == "1985-04-12T21:20:50.052Z" + inst.toEpochMilli() == 482196050052 + inst.toString() == "1985-04-12T23:20:50.052Z" } void "test parseRfc5424Timestamp ex2"() { @@ -80,7 +80,7 @@ class SyslogParserRfc5424Test extends Specification { then: inst.toEpochMilli() == 482196050052 - //inst.toString() == "1985-04-12T23:20:50.052Z" + inst.toString() == "1985-04-12T23:20:50.052Z" } void "test parseRfc5424Timestamp ex3"() { @@ -91,8 +91,8 @@ class SyslogParserRfc5424Test extends Specification { Instant inst = syslogParser.parseTimestamp(dateString) then: - inst.toEpochMilli() == 1065903255003 - //inst.toString() == "2003-10-11T20:14:15.003Z" + inst.toEpochMilli() == 1065910455003 + inst.toString() == "2003-10-11T22:14:15.003Z" } void "test parseRfc5424Timestamp ex4"() { @@ -104,7 +104,7 @@ class SyslogParserRfc5424Test extends Specification { then: inst.toEpochMilli() == 1061727255003 - //inst.toString() == "2003-08-24T12:14:15.003Z" + inst.toString() == "2003-08-24T12:14:15.003Z" } void "test parseRfc5424Timestamp ex5"() { @@ -116,7 +116,7 @@ class SyslogParserRfc5424Test extends Specification { then: inst.toEpochMilli() == 1061727255003 - //inst.toString() == "2003-08-24T12:14:15.003Z" + inst.toString() == "2003-08-24T12:14:15.003Z" } } diff --git a/src/test/resources/simplelogger.properties b/src/test/resources/simplelogger.properties index 5a917ef..876e5b5 100644 --- a/src/test/resources/simplelogger.properties +++ b/src/test/resources/simplelogger.properties @@ -1,4 +1,4 @@ -org.slf4j.simpleLogger.logFile=System.out +org.slf4j.simpleLogger.logFile=System.err org.slf4j.simpleLogger.showDateTime=false org.slf4j.simpleLogger.showShortLogName=true org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS