syslog: handle potential multiline syslog input

This commit is contained in:
Johannes Zellner
2024-05-15 13:17:02 +02:00
parent a1f2b5b696
commit 890de53b0a
3 changed files with 25 additions and 19 deletions
+19 -13
View File
@@ -12,7 +12,7 @@ const debug = require('debug')('syslog:server'),
net = require('net'),
path = require('path'),
paths = require('./src/paths.js'),
parser = require('nsyslog-parser'),
parser = require('nsyslog-parser-2'),
util = require('util');
let gServer = null;
@@ -30,22 +30,28 @@ async function start() {
gServer.on('connection', function (socket) {
socket.on('data', function (msg) {
const info = parser(msg.toString());
const lines = msg.toString().split('\n'); // may be multiline data
if (!info || !info.appName) return debug('Ignore unknown app log:', msg.toString());
lines.forEach(function (msg) {
if (!msg) return;
// remove line breaks to avoid holes in the log file
// we do not ignore empty log lines, to allow gaps for potential ease of readability
const message = info.message.replace(/\n/g, '');
const info = parser(msg);
const appLogDir = path.join(paths.LOG_DIR, info.appName);
if (!info || !info.appName) return debug('Ignore unknown app log:', msg);
try {
fs.mkdirSync(appLogDir, { recursive: true });
fs.appendFileSync(`${appLogDir}/app.log`, info.ts.toISOString() + ' ' + message + '\n');
} catch (error) {
console.error(error);
}
// remove line breaks to avoid holes in the log file
// we do not ignore empty log lines, to allow gaps for potential ease of readability
const message = info.message.replace(/\n/g, '');
const appLogDir = path.join(paths.LOG_DIR, info.appName);
try {
fs.mkdirSync(appLogDir, { recursive: true });
fs.appendFileSync(`${appLogDir}/app.log`, info.ts.toISOString() + ' ' + message + '\n');
} catch (error) {
console.error(error);
}
});
});
socket.on('error', function (error) {