Created
January 12, 2018 16:05
-
-
Save jsdevel/565bb0a1e6b60fb4f3628b8c51681465 to your computer and use it in GitHub Desktop.
Node.js slack logger example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { IncomingWebhook } = require('@slack/client'); | |
let sendToSlack; | |
const t = () => (new Date()).toISOString(); | |
if (process.env.SLACK_LOGGING_WEBHOOK_URL) { | |
const slack = new IncomingWebhook(process.env.SLACK_LOGGING_WEBHOOK_URL); | |
const handler = err => { | |
if (err) { | |
console.error(`Error sending message to slack: ${err}`); | |
} | |
}; | |
sendToSlack = args => { | |
slack.send({ | |
iconEmoji: ':nodejs:', | |
username: `${process.env.API_ENDPOINT} NODE_ENV=${process.env.NODE_ENV}`, | |
text: args.join(' ') | |
}, handler); | |
}; | |
} else { | |
sendToSlack = () => {}; | |
} | |
module.exports = { | |
error: (err, ...args) => { | |
const msg = [ | |
(err && err.status) || '', | |
(err && err.stack && `\n${err.stack}\n`) || err, | |
...args | |
]; | |
sendToSlack([':red-bar:', t(), ...msg]); | |
console.error(...msg); | |
}, | |
log: (...args) => { | |
sendToSlack([':grey-bar:', t(), ...args]); | |
console.log(...args); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment