Commit 683558ad authored by CompileNix's avatar CompileNix

complete #1: add expression based message filter / templating for customized messages

parent 2d5f0144
......@@ -4,8 +4,8 @@ class Config {
'test.txt',
'test1.txt'
];
this.defaultMessageTemplateFilter = /^\[((\d{1,2}\/\w{3}\/\d{4}):(\d{2}:\d{2}:\d{2}) ([+-]\d{4}))\] status:(\d{3}) request_time:(\d{1,}.\d{3}) upstream_response_time:(\d{1,}.\d{3}) bytes_sent:(\d{1,}) client_ip:(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b|(:{0,2}[\da-f]{1,4}){1,8}) domain:([\w.-]*) request:"((\w{3,7}) (\/[\w-.,_~:<>\\/[\]%@!$'()*+;?=&#]*) HTTP\/(\d\.\d))" referer:"([\w-.,_~:<>\\/[\]%@!$'()*+;?=&#]*)" user_agent:"([\w-.,_~: \/[\]%@!$'()*+;?=&#]*)"$/g
this.defaultMessageTemplateFilterMatchingGroupNames = {
this.defaultMessageTemplateFilter = /^\[((\d{1,2}\/\w{3}\/\d{4}):(\d{2}:\d{2}:\d{2}) ([+-]\d{4}))\] status:(\d{3}) request_time:(\d{1,}.\d{3}) upstream_response_time:(\d{1,}.\d{3}) bytes_sent:(\d{1,}) client_ip:(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b|(:{0,2}[\da-f]{1,4}){1,8}) domain:([\w.-]*) request:"((\w{3,7}) (\/[\w-.,_~:<>\\/[\]%@!$'()*+;?=&#]*) HTTP\/(\d\.\d))" referer:"([\w-.,_~:<>\\/[\]%@!$'()*+;?=&#]*)" user_agent:"([\w-.,_~: \/[\]%@!$'()*+;?=&#]*)"$/;
this.defaultMatchingGroupName = {
All: 0,
DateTime: 1,
Date: 2,
......@@ -26,11 +26,16 @@ class Config {
};
this.expressions = [
{
match: /HTTP\/\d\.\d\" 404/g,
subject: 'HTTP 404: ',
//messageTemplateFilter: /()/g
match: /status:404/,
//filter: (/** @type {string} */ line) => { return line.match(/./); },
subject: (/** @type {any} */ match) => {
return `HTTP ${match[this.defaultMatchingGroupName.StatusCode]}: `;
},
template: (/** @type {any} */ match) => {
return `${match[this.defaultMatchingGroupName.Domain]} \`"${match[this.defaultMatchingGroupName.Request]}"\``;
}
},
{ match: /HTTP\/\d\.\d\" 5\d{2}/g, subject: 'HTTP 5xx: ' }
{ match: /status:5\d{2}/, subject: 'HTTP 5xx: ' }
];
this.enableEmail = false;
......
......@@ -9,7 +9,6 @@ const Slack = require('slack-node');
let transporter;
let slack = new Slack();
let mailOptions = {};
let lineCounter = 0;
const fileWatchers = {};
let messages = new Array();
let queueWorkerRunning = false;
......@@ -26,12 +25,31 @@ async function notificationQueueWorker() {
continue;
}
let filterExpression = config.defaultMessageTemplateFilter;
if (message.expression.filter) {
filterExpression = message.expression.filter;
}
let match = message.message.match(filterExpression);
if (match === null) match = message.message;
const oldSubject = message.expression.subject;
if (typeof (message.expression.subject) !== 'function') {
var oldSubjectValue = message.expression.subject.toString();
message.expression.subject = () => oldSubjectValue;
}
const oldTemplate = message.expression.template;
if (typeof (message.expression.template) !== 'function') {
message.expression.template = () => `\`${message.message}\``;
}
if (config.enableEmail) {
await sendMail({
from: config.mailfrom,
to: config.mailto,
subject: `${config.subjectPrefix}`,
text: `${message.expression.subject}${message.message}`
subject: `${config.subjectPrefix}${message.expression.subject(match)}`,
text: message.expression.template(match)
});
}
......@@ -39,12 +57,14 @@ async function notificationQueueWorker() {
slack.webhook({
channel: config.slackChannel,
username: config.slackUserName,
text: `${message.expression.subject}${message.message}`
text: `${message.expression.subject(match)}${message.expression.template(match)}`
}, (err, response) => {
if (config.debug && err) console.log(err, response);
});
}
message.expression.subject = oldSubjectValue;
message.expression.template = oldTemplate;
await sleep(1000);
}
......@@ -57,7 +77,6 @@ async function filterLog( /** @type {string} */ line) {
expression.match.lastIndex = 0;
if (expression.match.test(line)) {
config.expressions[index].matchCounter++;
messages.push({
expression: expression,
message: line
......@@ -86,11 +105,6 @@ async function setupTail( /** @type {string[]} */ filesToWatch) {
process.exit(1);
}
for (let index = 0; index < config.expressions.length; index++) {
config.expressions[index].matchCounter = 0;
config.expressions[index].lastMatchContent = '';
}
for (const fileName of filesToWatch) {
if (!(await fs.exists(fileName))) {
console.log(`File not found, not watching: ${fileName}`);
......@@ -105,7 +119,6 @@ async function setupTail( /** @type {string[]} */ filesToWatch) {
});
tail.on('line', ( /** @type {string} */ data) => {
lineCounter++;
filterLog(data);
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment