This log handler will send each entry with a log level at or above a configured threshold as a message to a configured Slack channel.
npm install --save @debugr/slack
import { Logger, LogLevel } from '@debugr/core';
import { SlackHandler } from '@debugr/slack';
const globalContext = {
applicationName: 'example',
};
const logger = new Logger(globalContext, [
new SlackHandler({
threshold: LogLevel.FATAL,
webhookUrl: 'your slack webhook url',
}),
]);
logger.fatal('Something failed miserably!');
The SlackHandler
constructor accepts a required options
object
with the following keys as the first argument:
Option | Type | Default | Description |
---|---|---|---|
webhookUrl |
string |
(required) | A Slack webhook URL; see the Slack API docs on how to obtain one. |
threshold |
LogLevel |
LogLevel.ERROR |
The lowest level of entries which will be posted to the configured channel. Any entries below this level will be ignored. |
channel |
string |
The Slack channel ID the message should be posted to. This only works with legacy Slack webhooks. | |
username |
string |
The slack username the message should be posted under. This only works with legacy Slack webhooks. | |
iconUrl |
string |
The URL of an icon to be used in place of the default icon. This only works with legacy Slack webhooks. | |
iconEmoji |
string |
An emoji code string to use in place of the default icon. This only works with legacy Slack webhooks. | |
errorCallback |
(err: Error) => void |
(see description) | A callback which will be called when sending a message to Slack fails. The default callback will simply log the error into the console. |
bodyMapper |
(entry: LogEntry) => Record<string, any> |
(see description) | A callback mapping the log entry to payload to be sent to the configured webhook URL. At a minimum the payload must include a text key with a string content. |