spee.ch/config/slackConfig.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook;
const winston = require('winston');
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
}
// 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;
// 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...');
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.');
}
};
};
module.exports = new SlackConfig();