SquadJS/log-parser/index.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-10-05 12:52:01 -05:00
import EventEmitter from 'events';
2020-05-15 12:42:39 -05:00
import async from 'async';
import moment from 'moment';
2020-10-25 09:24:48 -05:00
import Logger from 'core/logger';
2020-05-15 12:42:39 -05:00
import TailLogReader from './log-readers/tail.js';
import FTPLogReader from './log-readers/ftp.js';
import rules from './rules/index.js';
2020-10-05 12:52:01 -05:00
export default class LogParser extends EventEmitter {
constructor(options = {}) {
super();
2020-05-15 12:42:39 -05:00
this.eventStore = {};
2020-10-14 11:01:19 -05:00
this.linesPerMinute = 0;
this.linesPerMinuteInterval = null;
this.queue = async.queue(async (line) => {
2020-10-25 09:28:36 -05:00
Logger.verbose('LogParser', 4, `Matching on line: ${line}`);
for (const rule of rules) {
const match = line.match(rule.regex);
if (!match) continue;
2020-10-25 09:28:36 -05:00
Logger.verbose('LogParser', 3, `Matched on line: ${match[0]}`);
match[1] = moment.utc(match[1], 'YYYY.MM.DD-hh.mm.ss:SSS').toDate();
match[2] = parseInt(match[2]);
2020-10-25 09:28:36 -05:00
rule.onMatch(match, this);
2020-10-25 09:28:36 -05:00
break;
}
2020-10-14 11:01:19 -05:00
this.linesPerMinute++;
});
2020-10-14 11:01:19 -05:00
2020-10-05 12:52:01 -05:00
switch (options.mode || 'tail') {
2020-05-15 12:42:39 -05:00
case 'tail':
this.logReader = new TailLogReader(this.queue.push, options);
2020-05-15 12:42:39 -05:00
break;
case 'ftp':
this.logReader = new FTPLogReader(this.queue.push, options);
2020-05-15 12:42:39 -05:00
break;
default:
throw new Error('Invalid mode.');
}
}
async watch() {
await this.logReader.watch();
2020-10-14 11:01:19 -05:00
this.linesPerMinuteInterval = setInterval(() => {
2020-10-25 09:24:48 -05:00
Logger.verbose('LogParser', 1, `Processing ${this.linesPerMinute} lines per minute.`);
2020-10-14 11:01:19 -05:00
this.linesPerMinute = 0;
}, 60 * 1000);
2020-05-15 12:42:39 -05:00
}
async unwatch() {
await this.logReader.unwatch();
2020-10-14 11:01:19 -05:00
clearInterval(this.linesPerMinuteInterval);
}
2020-05-15 12:42:39 -05:00
}