syslog: change it to unix domain socket

docker is using a extra udp port for every container. when there is
a lot of containers, a lot of random udp ports get used up. this causes
problems when installing apps that require contiguous port ranges
This commit is contained in:
Girish Ramakrishnan
2024-03-21 17:30:50 +01:00
parent 8e07b3c96d
commit 104997d77c
10 changed files with 40 additions and 34 deletions
+26 -20
View File
@@ -9,8 +9,8 @@ const LOG_FILENAME = 'app.log';
const assert = require('assert'),
debug = require('debug')('syslog:server'),
dgram = require('dgram'),
fs = require('fs'),
net = require('net'),
path = require('path'),
parser = require('nsyslog-parser'),
util = require('util');
@@ -19,42 +19,48 @@ let server = null;
async function start(options) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof options.port, 'number');
assert.strictEqual(typeof options.logFolder, 'string');
debug('==========================================');
debug(' Cloudron Syslog Daemon ');
debug('==========================================');
server = dgram.createSocket('udp4');
server = net.createServer();
server.on('error', function (error) {
console.error(`socket error: ${error}`);
console.error(`server error: ${error}`);
});
server.on('message', function (msg /*, rinfo */) {
const info = parser(msg.toString());
server.on('connection', function (socket) {
socket.on('data', function (msg) {
const info = parser(msg.toString());
if (!info || !info.appName) return debug('Ignore unknown app log:', msg.toString());
if (!info || !info.appName) return debug('Ignore unknown app log:', msg.toString());
// 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, '');
// 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 filePath = path.join(options.logFolder, info.appName);
const fileName = path.join(filePath, LOG_FILENAME);
const filePath = path.join(options.logFolder, info.appName);
const fileName = path.join(filePath, LOG_FILENAME);
try {
fs.mkdirSync(filePath, { recursive: true });
fs.appendFileSync(fileName, info.ts.toISOString() + ' ' + message + '\n');
} catch (error) {
console.error(error);
}
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}`);
});
});
await util.promisify(server.bind.bind(server))(options.port); // intentional double "bind"
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');
debug(`Listening on port ${options.port}`);
debug('Listening on syslog.sock');
}
async function stop() {