Files
logAggregator/logAggregator.js

55 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-01-04 11:06:37 -06:00
const micro = require('micro');
const { json, send } = require('micro');
const { RuleEngine } = require('./RuleEngine');
const fs = require('fs');
const jsonminify = require('jsonminify');
const winston = require("winston");
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(info => {
return `${info.timestamp} ${info.level}: ${info.message}`;
})
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: './log/LogAggregatorError.log', level: 'error' }),
new winston.transports.File({ filename: './log/Combined.log' })
]
});
2019-01-04 12:46:40 -06:00
const ruleEngine = new RuleEngine("*");
2019-01-07 08:58:16 -06:00
logger.log("info", "Service Starting logAggregator v0.1 by j.tretter@gmail.com");
2019-01-04 11:06:37 -06:00
const server = micro(async (req, res) => {
const js = await json(req);
try {
2019-01-07 07:51:40 -06:00
allInputLines = JSON.parse(js);
2019-01-04 11:06:37 -06:00
} catch (ex) {
2019-01-07 07:51:40 -06:00
logger.log("error", "Input not in JSON format:" + js);
send(res, 401, "Input not in JSON Format");
2019-01-04 11:06:37 -06:00
}
2019-01-07 07:51:40 -06:00
for(i=0;i<allInputLines.length;i++){
let theInputLine=allInputLines[i];
let isWhiteListed = ruleEngine.processData(theInputLine);
let miniInput = jsonminify(JSON.stringify(theInputLine));
let now = Date();
if (isWhiteListed) {
logger.log("debug", "Not Whitelisted: " + miniInput);
fs.appendFileSync("./log/skipped.log", miniInput + "\n");
process.stdout.write(".");
2019-01-07 07:51:40 -06:00
} else {
logger.log("debug", "Not Whitelisted: " + miniInput);
2019-01-07 07:51:40 -06:00
fs.appendFileSync("./log/issues.log", miniInput + "\n");
process.stdout.write("!");
2019-01-07 07:51:40 -06:00
}
2019-01-04 12:46:40 -06:00
}
2019-01-04 11:06:37 -06:00
2019-01-07 08:58:16 -06:00
send(res, 200, "OK");
2019-01-04 11:06:37 -06:00
});
server.listen(2222);