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-29 21:46:20 +02:00
this.update = (config) => {
2018-03-13 03:26:03 +01:00
if (!config) {
2018-03-29 21:33:52 +02:00
return winston.warn('No slack config received');
2018-03-13 03:26:03 +01:00
}
// update variables
2018-03-29 21:33:52 +02:00
winston.info('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:',
});
};
2018-05-02 19:30:33 +02:00
if (this.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-29 21:33:52 +02:00
winston.info('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();