A Slack transport for Winston 3+ that logs to a channel via webhooks.
npm install winston winston-slack-transport-proxyfied
const winston = require("winston");
const SlackHook = require("winston-slack-transport-proxyfied");
const logger = winston.createLogger({
level: "info",
transports: [
new SlackHook({
webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx"
})
]
});
logger.info("This should now appear on Slack");
const winston = require("winston");
const SlackHook = require("winston-slack-transport-proxyfied");
const logger = winston.createLogger({});
logger.add(SlackHook, {webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx"});
webhookUrl
- Slack incoming webhook URL. This can be from a basic integration or a bot. REQUIREDchannel
- Slack channel to post message to.username
- Username to post message with.iconEmoji
- Status icon to post message with. (interchangeable withiconUrl
)iconUrl
- Status icon to post message with. (interchangeable withiconEmoji
)formatter
- Custom function to format messages with. This function accepts theinfo
object (see Winston documentation) and must return an object with at least one of the following three keys:text
(string),attachments
(array of attachment objects),blocks
(array of layout block objects). These will be used to structure the format of the logged Slack message. By default, messages will use the format of[level]: [message]
with no attachments or layout blocks.level
- Level to log. Global settings will apply if this is blank.unfurlLinks
- Enables or disables link unfurling. (Default: false)unfurlMedia
- Enables or disables media unfurling. (Default: false)mrkdwn
- Enables or disablesmrkdwn
formatting within attachments or layout blocks (Default: false)proxy
- proxy server URL
winston-slack-webhook-transport
supports the ability to format messages using Slack's message layout features. To do this, supply a custom formatter that supplies the requisite object structure to create the desired layout.
const winston = require("winston");
const SlackHook = require("winston-slack-transport-proxyfied");
const logger = winston.createLogger({
level: "info",
transports: [
new SlackHook({
webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx",
formatter: info => {
return {
text: `${info.level}: ${info.message}`,
attachments: [
{
text: "Or don't pass anything. That's fine too"
}
],
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: "You can pass more info to the formatter by supplying additional parameters in the logger call"
}
}
]
}
}
})
]
});
logger.info("Definitely try playing around with this.")
const winston = require("winston");
const SlackHook = require("winston-slack-transport-proxyfied");
const logger = winston.createLogger({
level: "info",
transports: [
new SlackHook({
webhookUrl: "https://hooks.slack.com/services/xxx/xxx/xxx",
proxy: "http://192.168.1.5" // TODO: replace with your proxy server URL
})
]
});