Skip to content

Instantly share code, notes, and snippets.

@jsdevel
Created January 12, 2018 16:05
Show Gist options
  • Save jsdevel/565bb0a1e6b60fb4f3628b8c51681465 to your computer and use it in GitHub Desktop.
Save jsdevel/565bb0a1e6b60fb4f3628b8c51681465 to your computer and use it in GitHub Desktop.
Node.js slack logger example
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