2018-03-27 21:32:07 +02:00
|
|
|
const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook;
|
|
|
|
const winston = require('winston');
|
|
|
|
|
2018-03-10 01:45:24 +01:00
|
|
|
function SlackConfig () {
|
|
|
|
this.slackWebHook = 'default';
|
|
|
|
this.slackErrorChannel = 'default';
|
|
|
|
this.slackInfoChannel = 'default';
|
2018-03-13 03:26:03 +01:00
|
|
|
this.configure = (config) => {
|
|
|
|
if (!config) {
|
2018-03-27 22:21:59 +02:00
|
|
|
return console.log('no slack config received');
|
2018-03-13 03:26:03 +01:00
|
|
|
}
|
2018-03-27 21:32:07 +02:00
|
|
|
// update variables
|
2018-03-27 22:21:59 +02:00
|
|
|
console.log('configuring slack logger...');
|
2018-03-13 03:26:03 +01:00
|
|
|
const {slackWebHook, slackErrorChannel, slackInfoChannel} = config;
|
|
|
|
this.slackWebHook = slackWebHook;
|
|
|
|
this.slackErrorChannel = slackErrorChannel;
|
|
|
|
this.slackInfoChannel = slackInfoChannel;
|
2018-03-27 21:32:07 +02:00
|
|
|
// update slack webhook settings
|
|
|
|
if (this.slackWebHook) {
|
|
|
|
// add a transport for errors to slack
|
|
|
|
if (this.slackErrorChannel) {
|
|
|
|
winston.add(winstonSlackWebHook, {
|
|
|
|
name : 'slack-errors-transport',
|
|
|
|
level : 'warn',
|
|
|
|
webhookUrl: this.slackWebHook,
|
|
|
|
channel : this.slackErrorChannel,
|
|
|
|
username : 'spee.ch',
|
|
|
|
iconEmoji : ':face_with_head_bandage:',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
if (slackInfoChannel) {
|
|
|
|
winston.add(winstonSlackWebHook, {
|
|
|
|
name : 'slack-info-transport',
|
|
|
|
level : 'info',
|
|
|
|
webhookUrl: this.slackWebHook,
|
|
|
|
channel : this.slackInfoChannel,
|
|
|
|
username : 'spee.ch',
|
|
|
|
iconEmoji : ':nerd_face:',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
// send test messages
|
2018-03-27 22:21:59 +02:00
|
|
|
console.log('testing slack logger...');
|
2018-03-27 21:32:07 +02:00
|
|
|
winston.error('Slack "error" logging is online.');
|
|
|
|
winston.info('Slack "info" logging is online.');
|
|
|
|
} else {
|
|
|
|
winston.warn('Slack logging is not enabled because no slackWebHook config var provided.');
|
|
|
|
}
|
2018-03-10 01:45:24 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = new SlackConfig();
|