54 lines
1.5 KiB
JavaScript
Executable File
54 lines
1.5 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
var dgram = require('dgram'),
|
|
fs = require('fs'),
|
|
path = require('path'),
|
|
mkdirp = require('mkdirp'),
|
|
parser = require('nsyslog-parser');
|
|
|
|
const LOG_FILE_FOLDER = '/home/yellowtent/platformdata/logs';
|
|
const LOG_FILE_NAME = 'app.log';
|
|
const PORT = 2514; // syslog is 514 so we prefix with 2
|
|
|
|
const argv = require('yargs').argv;
|
|
|
|
if (argv.version) {
|
|
console.log('1.0.0');
|
|
process.exit(0);
|
|
}
|
|
|
|
console.log();
|
|
console.log('==========================================');
|
|
console.log(' Cloudron Syslog Daemon ');
|
|
console.log('==========================================');
|
|
console.log();
|
|
console.log(' Log Folder: ', LOG_FILE_FOLDER);
|
|
console.log(' UDP Port: ', PORT);
|
|
console.log();
|
|
console.log('==========================================');
|
|
console.log();
|
|
|
|
var server = dgram.createSocket('udp4');
|
|
|
|
server.on('error', function (error) {
|
|
console.error('Error:', error);
|
|
}).on('listening', function () {
|
|
console.log('Listening...');
|
|
}).on('message', function (msg, rinfo) {
|
|
var info = parser(msg.toString());
|
|
|
|
if (!info || !info.appName) return console.log('Ignore unknown app log:', msg.toString());
|
|
|
|
const filePath = path.join(LOG_FILE_FOLDER, info.appName);
|
|
const fileName = path.join(filePath, LOG_FILE_NAME);
|
|
|
|
try {
|
|
mkdirp.sync(filePath);
|
|
fs.appendFileSync(fileName, info.ts.toISOString() + ' ' + info.message);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}).bind(PORT);
|