From 915f55c0fb1c994117a77d71c22b85db0098c1be Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 13 Sep 2017 15:41:52 -0700 Subject: [PATCH] added winston-slack-webhook npm package --- config/custom-environment-variables.json | 3 +++ config/{loggerSetup.js => loggerConfig.js} | 4 ++++ config/slackLoggerConfig.js | 26 ++++++++++++++++++++++ package.json | 3 ++- speech.js | 5 +++-- 5 files changed, 38 insertions(+), 3 deletions(-) rename config/{loggerSetup.js => loggerConfig.js} (84%) create mode 100644 config/slackLoggerConfig.js diff --git a/config/custom-environment-variables.json b/config/custom-environment-variables.json index dd32d682..2ee10c42 100644 --- a/config/custom-environment-variables.json +++ b/config/custom-environment-variables.json @@ -5,5 +5,8 @@ "Database": { "Username": "MYSQL_USERNAME", "Password": "MYSQL_PASSWORD" + }, + "Logging": { + "SlackWebHook": "SLACK_WEB_HOOK" } } \ No newline at end of file diff --git a/config/loggerSetup.js b/config/loggerConfig.js similarity index 84% rename from config/loggerSetup.js rename to config/loggerConfig.js index ffd4c970..4efbbb02 100644 --- a/config/loggerSetup.js +++ b/config/loggerConfig.js @@ -12,6 +12,10 @@ module.exports = (winston, logLevel) => { ], }); + // winston.on('error', (err) => { + // console.log('unhandled exception in winston >> ', err); + // }); + winston.error('Level 0'); winston.warn('Level 1'); winston.info('Level 2'); diff --git a/config/slackLoggerConfig.js b/config/slackLoggerConfig.js new file mode 100644 index 00000000..3e5c615b --- /dev/null +++ b/config/slackLoggerConfig.js @@ -0,0 +1,26 @@ +const config = require('config'); +const SLACK_WEB_HOOK = config.get('Logging.SlackWebHook'); + +const winstonSlackWebHook = require('winston-slack-webhook').SlackWebHook; + +module.exports = (winston) => { + // add a transport for errors + winston.add(winstonSlackWebHook, { + name : 'slack-errors-transport', + level : 'error', + webhookUrl: SLACK_WEB_HOOK, + channel : '#speech-errors', + username : 'errorBot', + iconEmoji : ':face_with_head_bandage:', + }); + winston.add(winstonSlackWebHook, { + name : 'slack-info-transport', + level : 'info', + webhookUrl: SLACK_WEB_HOOK, + channel : '#speech-logs', + username : 'infoBot', + iconEmoji : ':nerd_face:', + }); + // send test message + winston.error('Testing slack logging... slack logging is online.'); +}; diff --git a/package.json b/package.json index 06a7de6c..ba075575 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "socket.io": "^2.0.1", "socketio-file-upload": "^0.6.0", "universal-analytics": "^0.4.13", - "winston": "^2.3.1" + "winston": "^2.3.1", + "winston-slack-webhook": "^0.1.5" }, "devDependencies": { "eslint": "3.19.0", diff --git a/speech.js b/speech.js index 259d0837..290de4e0 100644 --- a/speech.js +++ b/speech.js @@ -14,7 +14,8 @@ const db = require('./models'); // require our models for syncing // configure logging const logLevel = config.get('Logging.LogLevel'); -require('./config/loggerSetup.js')(logger, logLevel); +require('./config/loggerConfig.js')(logger, logLevel); +require('./config/slackLoggerConfig.js')(logger); // trust the proxy to get ip address for us app.enable('trust proxy'); @@ -154,5 +155,5 @@ db.sequelize }); }) .catch((error) => { - logger.error('Startup Error >>', error); + logger.error('Startup Error', error); });