2018-06-25 00:28:42 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2022-01-13 16:25:43 -08:00
|
|
|
start,
|
|
|
|
|
stop
|
2018-06-25 00:28:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const LOG_FILENAME = 'app.log';
|
|
|
|
|
|
2022-01-13 16:25:43 -08:00
|
|
|
const assert = require('assert'),
|
2023-04-14 19:31:45 +02:00
|
|
|
debug = require('debug')('syslog:server'),
|
2018-06-25 00:28:42 +02:00
|
|
|
fs = require('fs'),
|
2024-03-21 17:30:50 +01:00
|
|
|
net = require('net'),
|
2018-06-25 00:28:42 +02:00
|
|
|
path = require('path'),
|
2023-04-14 19:31:45 +02:00
|
|
|
parser = require('nsyslog-parser'),
|
|
|
|
|
util = require('util');
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2022-01-13 16:25:43 -08:00
|
|
|
let server = null;
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2023-04-14 19:31:45 +02:00
|
|
|
async function start(options) {
|
2018-06-25 00:28:42 +02:00
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
assert.strictEqual(typeof options.logFolder, 'string');
|
2023-04-14 19:31:45 +02:00
|
|
|
|
|
|
|
|
debug('==========================================');
|
|
|
|
|
debug(' Cloudron Syslog Daemon ');
|
|
|
|
|
debug('==========================================');
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
server = net.createServer();
|
2018-06-25 00:28:42 +02:00
|
|
|
|
|
|
|
|
server.on('error', function (error) {
|
2024-03-21 17:30:50 +01:00
|
|
|
console.error(`server error: ${error}`);
|
2023-04-14 19:31:45 +02:00
|
|
|
});
|
|
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
server.on('connection', function (socket) {
|
|
|
|
|
socket.on('data', function (msg) {
|
|
|
|
|
const info = parser(msg.toString());
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
if (!info || !info.appName) return debug('Ignore unknown app log:', msg.toString());
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
// 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, '');
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
const filePath = path.join(options.logFolder, info.appName);
|
|
|
|
|
const fileName = path.join(filePath, LOG_FILENAME);
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(filePath, { recursive: true });
|
|
|
|
|
fs.appendFileSync(fileName, info.ts.toISOString() + ' ' + message + '\n');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('error', function (error) {
|
|
|
|
|
console.error(`socket error: ${error}`);
|
|
|
|
|
});
|
2023-04-14 19:31:45 +02:00
|
|
|
});
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
await fs.promises.rm('/home/yellowtent/platformdata/logs/syslog.sock', { force: true });
|
|
|
|
|
await util.promisify(server.listen.bind(server))('/home/yellowtent/platformdata/logs/syslog.sock');
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2024-03-21 17:30:50 +01:00
|
|
|
debug('Listening on syslog.sock');
|
2023-04-14 19:31:45 +02:00
|
|
|
}
|
2018-06-25 00:28:42 +02:00
|
|
|
|
2023-04-14 19:31:45 +02:00
|
|
|
async function stop() {
|
|
|
|
|
if (!server) return;
|
|
|
|
|
await util.promisify(server.close.bind(server))();
|
2018-06-25 00:28:42 +02:00
|
|
|
server = null;
|
2023-04-14 19:31:45 +02:00
|
|
|
}
|