From 1e957303607bc24c70cf0be8c38d550889a1dc2f Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Tue, 6 Oct 2020 05:33:08 +0200 Subject: [PATCH] Improve rfc3164 parser. --- .../biz/nellemann/syslogd/SyslogParser.java | 2 +- .../nellemann/syslogd/SyslogParserTest.groovy | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/biz/nellemann/syslogd/SyslogParser.java b/src/main/java/biz/nellemann/syslogd/SyslogParser.java index 528cfd6..372b516 100644 --- a/src/main/java/biz/nellemann/syslogd/SyslogParser.java +++ b/src/main/java/biz/nellemann/syslogd/SyslogParser.java @@ -37,7 +37,7 @@ public class SyslogParser { public static SyslogMessage parseRfc3164(final String input) throws NumberFormatException { - Pattern pattern = Pattern.compile("^<(\\d{1,3})>(\\D{3}\\s+\\d{1,2} \\d{2}:\\d{2}:\\d{2})\\s+(Message forwarded from \\S+:|\\S+)\\s+(\\S+): (.*)", Pattern.CASE_INSENSITIVE); + Pattern pattern = Pattern.compile("^<(\\d{1,3})>(\\D{3}\\s+\\d{1,2} \\d{2}:\\d{2}:\\d{2})\\s+(Message forwarded from \\S+:|\\S+)\\s+([^\\s:]+):?\\s+(.*)", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); boolean matchFound = matcher.find(); if(!matchFound) { diff --git a/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy b/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy index 9dd21bd..403337e 100644 --- a/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy +++ b/src/test/groovy/biz/nellemann/syslogd/SyslogParserTest.groovy @@ -2,6 +2,8 @@ package biz.nellemann.syslogd import spock.lang.Specification import java.time.Instant +import java.time.OffsetDateTime; + class SyslogParserTest extends Specification { void "test rfc5424 message"() { @@ -57,7 +59,6 @@ class SyslogParserTest extends Specification { msg.message == "mark : TTY=pts/1 ; PWD=/etc/rsyslog.d ; USER=root ; COMMAND=/usr/sbin/service rsyslog restart" } - /* void "test gdm-session message"() { setup: String input = "<12>Oct 5 18:31:01 xps13 /usr/lib/gdm3/gdm-x-session[1921]: (EE) event5 - CUST0001:00 06CB:76AF Touchpad: kernel bug: Touch jump detected and discarded." @@ -68,8 +69,19 @@ class SyslogParserTest extends Specification { then: msg.application == "/usr/lib/gdm3/gdm-x-session[1921]" msg.message == "(EE) event5 - CUST0001:00 06CB:76AF Touchpad: kernel bug: Touch jump detected and discarded." - }*/ + } + void "test intellij message"() { + setup: + String input = "<14>Oct 6 05:10:26 xps13 com.jetbrains.IntelliJ-IDEA-Ulti git4idea.commands.GitStandardProgressAnalyzer\$1.onLineAvailable(GitStandardProgressAnalyzer.java:45)" + + when: + SyslogMessage msg = SyslogParser.parseRfc3164(input) + + then: + msg.application == "com.jetbrains.IntelliJ-IDEA-Ulti" + msg.message == "git4idea.commands.GitStandardProgressAnalyzer\$1.onLineAvailable(GitStandardProgressAnalyzer.java:45)" + } void "test parseRfc3164Timestamp"() { @@ -99,5 +111,4 @@ class SyslogParserTest extends Specification { } -import java.time.OffsetDateTime;