From dc3fcb0e096e3539c867c6ab14860970ea3dae4b Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Tue, 26 Jan 2021 21:44:23 +0100 Subject: [PATCH] Refactor forwarding logic. --- build.gradle | 2 +- gradle.properties | 2 +- slf4j.simpleLogger.log | 2 + .../biz/nellemann/syslogd/Application.java | 52 +++++++++++++------ .../biz/nellemann/syslogd/SyslogParser.java | 4 +- .../biz/nellemann/syslogd/SyslogPrinter.java | 5 +- src/main/resources/simplelogger.properties | 4 +- 7 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 slf4j.simpleLogger.log diff --git a/build.gradle b/build.gradle index 5515c6e..f68bf6c 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ dependencies { implementation 'org.slf4j:slf4j-simple:1.7.30' testImplementation('org.spockframework:spock-core:2.0-M4-groovy-3.0') - testImplementation("org.slf4j:slf4j-simple:1.7.+") + testImplementation("org.slf4j:slf4j-simple:1.7.30") } application { diff --git a/gradle.properties b/gradle.properties index ac4b643..bdcfd3b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ id = syslogd group = biz.nellemann.syslogd -version = 1.0.8 +version = 1.0.9 diff --git a/slf4j.simpleLogger.log b/slf4j.simpleLogger.log new file mode 100644 index 0000000..7a0ab5b --- /dev/null +++ b/slf4j.simpleLogger.log @@ -0,0 +1,2 @@ +[SyslogServer] [DEBUG] SyslogParser - getFacility() - 68 => 8 +[SyslogServer] [DEBUG] SyslogParser - getSeverity() - 68 => 4 diff --git a/src/main/java/biz/nellemann/syslogd/Application.java b/src/main/java/biz/nellemann/syslogd/Application.java index b6ffb81..1eaf712 100644 --- a/src/main/java/biz/nellemann/syslogd/Application.java +++ b/src/main/java/biz/nellemann/syslogd/Application.java @@ -15,13 +15,15 @@ */ package biz.nellemann.syslogd; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.slf4j.impl.SimpleLogger; + import picocli.CommandLine; import picocli.CommandLine.Command; import java.io.IOException; import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Command(name = "syslogd", mixinStandardHelpOptions = true, @@ -29,7 +31,9 @@ import java.util.concurrent.Callable; versionProvider = biz.nellemann.syslogd.VersionProvider.class) public class Application implements Callable, LogListener { - private final static Logger log = LoggerFactory.getLogger(Application.class); + private boolean doForward = false; + private UdpClient udpClient; + @CommandLine.Option(names = {"-p", "--port"}, description = "Listening port [default: 514].", defaultValue = "514") private int port; @@ -49,22 +53,38 @@ public class Application implements Callable, LogListener { @CommandLine.Option(names = "--rfc5424", description = "Parse RFC-5424 messages [default: RFC-3164].", defaultValue = "false") private boolean rfc5424; - @CommandLine.Option(names = { "-f", "--forward"}, description = "Forward messages (UDP RFC-3164) [default: false].", defaultValue = "false") - private boolean forward; + @CommandLine.Option(names = { "-f", "--forward"}, description = "Forward to UDP host[:port] (RFC-3164).", paramLabel = "") + private String forward; - @CommandLine.Option(names = "--forward-host", description = "Forward to host [default: localhost].", paramLabel = "", defaultValue = "localhost") - private String forwardHost; + @CommandLine.Option(names = { "-d", "--debug" }, description = "Enable debugging [default: 'false'].") + private boolean enableDebug = false; - @CommandLine.Option(names = "--forward-port", description = "Forward to port [default: 1514].", paramLabel = "", defaultValue = "1514") - private int forwardPort; - - private UdpClient udpClient; @Override public Integer call() throws IOException { - if(forward) { - udpClient = new UdpClient(forwardHost, forwardPort); + if(enableDebug) { + System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG"); + } + + if(forward != null && !forward.isEmpty()) { + String fHost, fPort; + Pattern pattern = Pattern.compile("^([^:]+)(?::([0-9]+))?$", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(forward); + if(matcher.find()) { + fHost = matcher.group(1); + if(matcher.groupCount() == 2 && matcher.group(2) != null) { + fPort = matcher.group(2); + } else { + fPort = "514"; + } + } else { + fHost = "localhost"; + fPort = "514"; + } + + udpClient = new UdpClient(fHost, Integer.parseInt(fPort)); + doForward = true; } if(udpServer) { @@ -96,7 +116,7 @@ public class Application implements Callable, LogListener { msg = SyslogParser.parseRfc3164(message); } } catch(Exception e) { - log.error("onLogEvent() - Error parsing message: ", e); + e.printStackTrace(); } if(msg != null) { @@ -109,11 +129,11 @@ public class Application implements Callable, LogListener { } } - if(forward) { + if(doForward) { try { udpClient.send(SyslogPrinter.toRfc3164(msg)); } catch (Exception e) { - log.error("onLogEvent()", e); + e.printStackTrace(); } } } diff --git a/src/main/java/biz/nellemann/syslogd/SyslogParser.java b/src/main/java/biz/nellemann/syslogd/SyslogParser.java index c464f17..93b1cc3 100644 --- a/src/main/java/biz/nellemann/syslogd/SyslogParser.java +++ b/src/main/java/biz/nellemann/syslogd/SyslogParser.java @@ -177,7 +177,7 @@ public class SyslogParser { int priority = Integer.parseInt(pri); int facility = priority >> 3; - log.debug("getFacility() - " + pri + " => " + facility); + //log.debug("getFacility() - " + pri + " => " + facility); return facility; } @@ -193,7 +193,7 @@ public class SyslogParser { int priority = Integer.parseInt(pri); int severity = priority & 0x07; - log.debug("getSeverity() - " + pri + " => " + severity); + //log.debug("getSeverity() - " + pri + " => " + severity); return severity; } diff --git a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java index 2396b58..6004afc 100644 --- a/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java +++ b/src/main/java/biz/nellemann/syslogd/SyslogPrinter.java @@ -1,8 +1,5 @@ package biz.nellemann.syslogd; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; - public class SyslogPrinter { public static String toString(SyslogMessage msg) { @@ -42,7 +39,7 @@ public class SyslogPrinter { public static String toRfc3164(SyslogMessage msg) { StringBuilder sb = new StringBuilder(); sb.append(getPri(msg.facility, msg.severity)); - sb.append(" " + new java.text.SimpleDateFormat("MMM dd HH:mm:ss").format(new java.util.Date(msg.timestamp.toEpochMilli()))); + sb.append(new java.text.SimpleDateFormat("MMM dd HH:mm:ss").format(new java.util.Date(msg.timestamp.toEpochMilli()))); sb.append(" " + msg.hostname); sb.append(" " + msg.application); sb.append(": " + msg.message); diff --git a/src/main/resources/simplelogger.properties b/src/main/resources/simplelogger.properties index bd2eeaa..d5b0b17 100644 --- a/src/main/resources/simplelogger.properties +++ b/src/main/resources/simplelogger.properties @@ -1,4 +1,4 @@ -org.slf4j.simpleLogger.showDateTime=true +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 org.slf4j.simpleLogger.levelInBrackets=true