From 8d32c853d5b943e729d84ca2e777a5ef43d70e64 Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Mon, 15 Sep 2025 13:33:41 +0200 Subject: [PATCH] syslog: use named captures and better logging --- syslog.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/syslog.js b/syslog.js index 7ca0ef331..94ccac3b5 100755 --- a/syslog.js +++ b/syslog.js @@ -20,22 +20,22 @@ let gServer = null; // example: <34>1 2023-09-07T14:33:22Z myhost myapp pid msgid [exampleSDID@32473 iut="3" eventSource="Application"] An example message // the structured data can be "-" when missing function parseRFC5424Message(rawMessage) { - const syslogRegex = /^<(\d+)>(\d+) (\S+) (\S+) (\S+) (\S+) (\S+) (?:\[(.*?)\]|-)?(.*)$/s; // /s means .* will match newline . (?: is non-capturing group + const syslogRegex = /^<(?\d+)>(?\d+) (?\S+) (?\S+) (?\S+) (?\S+) (?\S+) (?:\[(?.*?)\]|-)(?.*)$/s; // /s means .* will match newline . (?: is non-capturing group const match = rawMessage.match(syslogRegex); if (!match) return null; - const [, pri, version, timestamp, hostname, appName, procId, msgId, structuredData, message] = match; + const { priority, version, timestamp, hostname, appName, procId, msgId, structuredData, message } = match.groups; return { - pri: parseInt(pri, 10), // priority + pri: parseInt(priority, 10), // priority version: parseInt(version, 10), // version timestamp, // timestamp hostname, // hostname appName, // app name procId, // process ID msgId, // message ID - structuredData: structuredData ? structuredData : null, // structured data (if present) + structuredData: structuredData || null, // structured data (if present) message // message }; } @@ -56,8 +56,8 @@ async function start() { const msg = data.toString('utf8').trim(); // strip any trailing empty new lines for (const line of msg.split('\n')) { // empirically, multiple messages can arrive in a single packet const info = parseRFC5424Message(line); - if (!info) return debug('Unable to parse:', msg); - if (!info.appName) return debug('Ignore unknown app:', msg); + if (!info) return debug(`Unable to parse: [${msg}]`); + if (!info.appName) return debug(`Ignore unknown app: [${msg}]`); const appLogDir = path.join(paths.LOG_DIR, info.appName);