2022-11-06 13:44:47 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2023-03-27 10:38:09 +02:00
|
|
|
const assert = require('assert'),
|
|
|
|
|
path = require('path'),
|
2024-02-22 18:08:32 +01:00
|
|
|
shell = require('./shell.js'),
|
2023-03-27 10:38:09 +02:00
|
|
|
spawn = require('child_process').spawn,
|
|
|
|
|
stream = require('stream'),
|
2022-11-06 13:44:47 +01:00
|
|
|
{ StringDecoder } = require('string_decoder'),
|
|
|
|
|
TransformStream = stream.Transform;
|
|
|
|
|
|
2023-03-27 10:38:09 +02:00
|
|
|
const LOGTAIL_CMD = path.join(__dirname, 'scripts/logtail.sh');
|
|
|
|
|
|
2022-11-06 13:44:47 +01:00
|
|
|
class LogStream extends TransformStream {
|
|
|
|
|
constructor(options) {
|
|
|
|
|
super();
|
|
|
|
|
this._options = Object.assign({ source: 'unknown', format: 'json' }, options);
|
|
|
|
|
this._decoder = new StringDecoder();
|
|
|
|
|
this._soFar = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_format(line) {
|
|
|
|
|
if (this._options.format !== 'json') return line + '\n';
|
|
|
|
|
|
|
|
|
|
const data = line.split(' '); // logs are <ISOtimestamp> <msg>
|
|
|
|
|
let timestamp = (new Date(data[0])).getTime();
|
|
|
|
|
if (isNaN(timestamp)) timestamp = 0;
|
|
|
|
|
const message = line.slice(data[0].length+1);
|
|
|
|
|
|
|
|
|
|
return JSON.stringify({
|
2024-02-22 18:08:32 +01:00
|
|
|
realtimeTimestamp: timestamp * 1000, // timestamp info can be missing (0) for app logs via logPaths
|
|
|
|
|
message: message || line, // send the line if message parsing failed
|
2022-11-06 13:44:47 +01:00
|
|
|
source: this._options.source
|
|
|
|
|
}) + '\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_transform(chunk, encoding, callback) {
|
|
|
|
|
const data = this._soFar + this._decoder.write(chunk);
|
|
|
|
|
let start = this._soFar.length, end = -1;
|
|
|
|
|
while ((end = data.indexOf('\n', start)) !== -1) {
|
|
|
|
|
const line = data.slice(start, end); // does not include end
|
|
|
|
|
this.push(this._format(line));
|
|
|
|
|
start = end + 1;
|
|
|
|
|
}
|
|
|
|
|
this._soFar = data.slice(start);
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_flush(callback) {
|
|
|
|
|
const line = this._soFar + this._decoder.end();
|
|
|
|
|
this.push(this._format(line));
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 10:38:09 +02:00
|
|
|
function tail(filePaths, options) {
|
|
|
|
|
assert(Array.isArray(filePaths));
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
|
2024-02-22 18:08:32 +01:00
|
|
|
const lines = options.lines === -1 ? '+1' : options.lines;
|
|
|
|
|
const args = [ LOGTAIL_CMD, '--lines=' + lines ];
|
|
|
|
|
if (options.follow) args.push('--follow');
|
2023-03-27 10:38:09 +02:00
|
|
|
|
2024-02-24 17:18:38 +01:00
|
|
|
return shell.sudo('tail', args.concat(filePaths), { streamStdout: true }, () => {});
|
2023-03-27 10:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function journalctl(unit, options) {
|
|
|
|
|
assert.strictEqual(typeof unit, 'string');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
|
2024-02-22 16:50:32 +01:00
|
|
|
const args = [
|
|
|
|
|
'--lines=' + (options.lines === -1 ? 'all' : options.lines),
|
|
|
|
|
`--unit=${unit}`,
|
|
|
|
|
'--no-pager',
|
|
|
|
|
'--output=short-iso'
|
|
|
|
|
];
|
2023-03-27 10:38:09 +02:00
|
|
|
|
|
|
|
|
if (options.follow) args.push('--follow');
|
|
|
|
|
|
|
|
|
|
return spawn('journalctl', args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
tail,
|
|
|
|
|
journalctl,
|
|
|
|
|
LogStream
|
|
|
|
|
};
|