Refactor forwarding logic.
This commit is contained in:
parent
86f645f295
commit
dc3fcb0e09
|
@ -18,7 +18,7 @@ dependencies {
|
||||||
implementation 'org.slf4j:slf4j-simple:1.7.30'
|
implementation 'org.slf4j:slf4j-simple:1.7.30'
|
||||||
|
|
||||||
testImplementation('org.spockframework:spock-core:2.0-M4-groovy-3.0')
|
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 {
|
application {
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
id = syslogd
|
id = syslogd
|
||||||
group = biz.nellemann.syslogd
|
group = biz.nellemann.syslogd
|
||||||
version = 1.0.8
|
version = 1.0.9
|
||||||
|
|
2
slf4j.simpleLogger.log
Normal file
2
slf4j.simpleLogger.log
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[SyslogServer] [DEBUG] SyslogParser - getFacility() - 68 => 8
|
||||||
|
[SyslogServer] [DEBUG] SyslogParser - getSeverity() - 68 => 4
|
|
@ -15,13 +15,15 @@
|
||||||
*/
|
*/
|
||||||
package biz.nellemann.syslogd;
|
package biz.nellemann.syslogd;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.impl.SimpleLogger;
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
import picocli.CommandLine.Command;
|
import picocli.CommandLine.Command;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@Command(name = "syslogd",
|
@Command(name = "syslogd",
|
||||||
mixinStandardHelpOptions = true,
|
mixinStandardHelpOptions = true,
|
||||||
|
@ -29,7 +31,9 @@ import java.util.concurrent.Callable;
|
||||||
versionProvider = biz.nellemann.syslogd.VersionProvider.class)
|
versionProvider = biz.nellemann.syslogd.VersionProvider.class)
|
||||||
public class Application implements Callable<Integer>, LogListener {
|
public class Application implements Callable<Integer>, 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")
|
@CommandLine.Option(names = {"-p", "--port"}, description = "Listening port [default: 514].", defaultValue = "514")
|
||||||
private int port;
|
private int port;
|
||||||
|
@ -49,22 +53,38 @@ public class Application implements Callable<Integer>, LogListener {
|
||||||
@CommandLine.Option(names = "--rfc5424", description = "Parse RFC-5424 messages [default: RFC-3164].", defaultValue = "false")
|
@CommandLine.Option(names = "--rfc5424", description = "Parse RFC-5424 messages [default: RFC-3164].", defaultValue = "false")
|
||||||
private boolean rfc5424;
|
private boolean rfc5424;
|
||||||
|
|
||||||
@CommandLine.Option(names = { "-f", "--forward"}, description = "Forward messages (UDP RFC-3164) [default: false].", defaultValue = "false")
|
@CommandLine.Option(names = { "-f", "--forward"}, description = "Forward to UDP host[:port] (RFC-3164).", paramLabel = "<host>")
|
||||||
private boolean forward;
|
private String forward;
|
||||||
|
|
||||||
@CommandLine.Option(names = "--forward-host", description = "Forward to host [default: localhost].", paramLabel = "<hostname>", defaultValue = "localhost")
|
@CommandLine.Option(names = { "-d", "--debug" }, description = "Enable debugging [default: 'false'].")
|
||||||
private String forwardHost;
|
private boolean enableDebug = false;
|
||||||
|
|
||||||
@CommandLine.Option(names = "--forward-port", description = "Forward to port [default: 1514].", paramLabel = "<port>", defaultValue = "1514")
|
|
||||||
private int forwardPort;
|
|
||||||
|
|
||||||
private UdpClient udpClient;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer call() throws IOException {
|
public Integer call() throws IOException {
|
||||||
|
|
||||||
if(forward) {
|
if(enableDebug) {
|
||||||
udpClient = new UdpClient(forwardHost, forwardPort);
|
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) {
|
if(udpServer) {
|
||||||
|
@ -96,7 +116,7 @@ public class Application implements Callable<Integer>, LogListener {
|
||||||
msg = SyslogParser.parseRfc3164(message);
|
msg = SyslogParser.parseRfc3164(message);
|
||||||
}
|
}
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
log.error("onLogEvent() - Error parsing message: ", e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(msg != null) {
|
if(msg != null) {
|
||||||
|
@ -109,11 +129,11 @@ public class Application implements Callable<Integer>, LogListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(forward) {
|
if(doForward) {
|
||||||
try {
|
try {
|
||||||
udpClient.send(SyslogPrinter.toRfc3164(msg));
|
udpClient.send(SyslogPrinter.toRfc3164(msg));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("onLogEvent()", e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,7 +177,7 @@ public class SyslogParser {
|
||||||
int priority = Integer.parseInt(pri);
|
int priority = Integer.parseInt(pri);
|
||||||
int facility = priority >> 3;
|
int facility = priority >> 3;
|
||||||
|
|
||||||
log.debug("getFacility() - " + pri + " => " + facility);
|
//log.debug("getFacility() - " + pri + " => " + facility);
|
||||||
return facility;
|
return facility;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,7 +193,7 @@ public class SyslogParser {
|
||||||
int priority = Integer.parseInt(pri);
|
int priority = Integer.parseInt(pri);
|
||||||
int severity = priority & 0x07;
|
int severity = priority & 0x07;
|
||||||
|
|
||||||
log.debug("getSeverity() - " + pri + " => " + severity);
|
//log.debug("getSeverity() - " + pri + " => " + severity);
|
||||||
return severity;
|
return severity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
package biz.nellemann.syslogd;
|
package biz.nellemann.syslogd;
|
||||||
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.time.temporal.ChronoUnit;
|
|
||||||
|
|
||||||
public class SyslogPrinter {
|
public class SyslogPrinter {
|
||||||
|
|
||||||
public static String toString(SyslogMessage msg) {
|
public static String toString(SyslogMessage msg) {
|
||||||
|
@ -42,7 +39,7 @@ public class SyslogPrinter {
|
||||||
public static String toRfc3164(SyslogMessage msg) {
|
public static String toRfc3164(SyslogMessage msg) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(getPri(msg.facility, msg.severity));
|
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.hostname);
|
||||||
sb.append(" " + msg.application);
|
sb.append(" " + msg.application);
|
||||||
sb.append(": " + msg.message);
|
sb.append(": " + msg.message);
|
||||||
|
|
|
@ -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.showShortLogName=true
|
||||||
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS
|
|
||||||
org.slf4j.simpleLogger.levelInBrackets=true
|
org.slf4j.simpleLogger.levelInBrackets=true
|
||||||
|
|
Loading…
Reference in a new issue