remove usage of nsyslog-parser-2

this module is somehow parsing the syslog incorrectly causing
incorrect directories being created in the logs directory
(since appName got parsed incorrectly)
This commit is contained in:
Girish Ramakrishnan
2024-09-10 12:59:42 +02:00
parent e5dcf78ceb
commit 8c0c9981de
4 changed files with 30 additions and 17 deletions
+29 -10
View File
@@ -12,11 +12,33 @@ const debug = require('debug')('syslog:server'),
net = require('net'),
path = require('path'),
paths = require('./src/paths.js'),
parser = require('nsyslog-parser-2'),
util = require('util');
let gServer = null;
// https://docs.docker.com/engine/logging/drivers/syslog/
// example: <34>1 2023-09-07T14:33:22Z myhost myapp 1234 5678 [exampleSDID@32473 iut="3" eventSource="Application"] An example message
function parseRFC5424Message(message) {
const syslogRegex = /^<(\d+)>(\d+) (\S+) (\S+) (\S+) (\S+) (\S+) (?:\[(.*?)\])?(.*)$/;
const match = message.match(syslogRegex);
if (!match) return null;
const [, pri, version, timestamp, hostname, appName, procId, msgId, structuredData, msg] = match;
return {
pri: parseInt(pri, 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)
msg: msg ? msg.trim() : null // message
};
}
async function start() {
debug('==========================================');
debug(' Cloudron Syslog Daemon ');
@@ -32,11 +54,8 @@ async function start() {
socket.on('data', function (msg) {
const lines = msg.toString().split('\n'); // may be multiline data
lines.forEach(function (msg) {
if (!msg) return;
const info = parser(msg);
for (const msg of lines) {
const info = parseRFC5424Message(msg);
if (!info || !info.appName) return debug('Ignore unknown app log:', msg);
// remove line breaks to avoid holes in the log file
@@ -47,15 +66,15 @@ async function start() {
try {
fs.mkdirSync(appLogDir, { recursive: true });
fs.appendFileSync(`${appLogDir}/app.log`, info.ts.toISOString() + ' ' + message + '\n');
fs.appendFileSync(`${appLogDir}/app.log`, `${info.timestamp} ${message}\n}`);
} catch (error) {
console.error(error);
debug(error);
}
});
}
});
socket.on('error', function (error) {
console.error(`socket error: ${error}`);
debug(`socket error: ${error}`);
});
});