From deeebdc5c87a40aad392d9bcc1e68ef1a2874f9d Mon Sep 17 00:00:00 2001 From: Will Toth Date: Mon, 4 Nov 2024 22:16:38 -0600 Subject: [PATCH] Add a basic syslog appender This commits allows photonvision to push logs to a remote syslog server such as the one on the roboRIO. To enable this on the RIO create a file in /etc/syslog-ng.d/ for example @version: 3.8 source s_net { tcp(ip(0.0.0.0) port(514) max-connections (5000)); udp(); }; destination d_syslog { file("/var/log/remotelogs/syslog"); }; log { source(s_net); destination(d_syslog); }; This is experimental --- photon-core/build.gradle | 3 ++ .../photonvision/common/logging/Logger.java | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/photon-core/build.gradle b/photon-core/build.gradle index 5cf4a2bb45..3d3c9bacee 100644 --- a/photon-core/build.gradle +++ b/photon-core/build.gradle @@ -37,6 +37,9 @@ dependencies { // Zip implementation 'org.zeroturnaround:zt-zip:1.14' + // Syslog + implementation "com.cloudbees:syslog-java-client:1.1.7" + implementation "org.xerial:sqlite-jdbc:3.41.0.0" implementation("org.photonvision:rknn_jni-jni:$rknnVersion:linuxarm64") { transitive = false diff --git a/photon-core/src/main/java/org/photonvision/common/logging/Logger.java b/photon-core/src/main/java/org/photonvision/common/logging/Logger.java index dc0ebefe72..1a8caa5cd8 100644 --- a/photon-core/src/main/java/org/photonvision/common/logging/Logger.java +++ b/photon-core/src/main/java/org/photonvision/common/logging/Logger.java @@ -29,6 +29,10 @@ import org.photonvision.common.dataflow.DataChangeService; import org.photonvision.common.dataflow.events.OutgoingUIEvent; import org.photonvision.common.util.TimedTaskManager; +import com.cloudbees.syslog.Facility; +import com.cloudbees.syslog.MessageFormat; +import com.cloudbees.syslog.Severity; +import com.cloudbees.syslog.sender.UdpSyslogMessageSender; @SuppressWarnings("unused") public class Logger { @@ -106,6 +110,7 @@ public static String format( static { currentAppenders.add(new ConsoleLogAppender()); currentAppenders.add(uiLogAppender); + currentAppenders.add(new SyslogAppender("SomeCoprocessor", "172.22.11.2")); addFileAppender(PathManager.getInstance().getLogPath()); cleanLogs(PathManager.getInstance().getLogsDir()); } @@ -314,6 +319,50 @@ public void log(String message, LogLevel level) { } } + private static class SyslogAppender implements LogAppender { + private UdpSyslogMessageSender messageSender = new UdpSyslogMessageSender(); + + private Severity ConvertLogLevel(LogLevel level) { + switch (level) { + case DEBUG: + return Severity.DEBUG; + case ERROR: + return Severity.ERROR; + case INFO: + return Severity.INFORMATIONAL; + case TRACE: + return Severity.DEBUG; + case WARN: + return Severity.WARNING; + default: + return Severity.INFORMATIONAL; + } + } + + public SyslogAppender(String hostname, String ip) { + this(hostname, ip, 514); + } + + public SyslogAppender(String hostname, String ip, int port) { + messageSender.setDefaultMessageHostname(hostname); + messageSender.setDefaultAppName("photonvision"); + messageSender.setDefaultFacility(Facility.USER); + messageSender.setDefaultSeverity(Severity.INFORMATIONAL); + messageSender.setSyslogServerHostname(ip); + messageSender.setSyslogServerPort(port); + messageSender.setMessageFormat(MessageFormat.RFC_5424); + } + + @Override + public void log(String message, LogLevel level) { + messageSender.setDefaultSeverity(ConvertLogLevel(level)); + try { + messageSender.sendMessage(message); + } catch (IOException e) { + } + } + } + private static class FileLogAppender implements LogAppender { private OutputStream out; private boolean wantsFlush;