slack-logs
v1.1.3
Published
slack-logs
Downloads
338
Maintainers
Readme
Slack Log Package
Description
This package provides a simple and efficient way to log messages directly to Slack from your Node.js applications. Utilizing Slack's Incoming Webhooks, it allows for real-time notifications and logging of critical events, errors, or any custom messages you choose to send to your Slack channel.
Features
- Easy integration with Slack via Incoming Webhooks.
- Supports custom messages with dynamic data.
- Validates Slack webhook URLs before attempting to send messages.
- Asynchronous logging with async/await support.
- Configurable to fit various logging needs.
Installation
- Install the package using npm:
npm install slack-logs
- Or using yarn:
yarn add slack-logs
Configuration:
Before you start, ensure you have created an Incoming Webhook in Slack and have the webhook URL ready. For more information on setting up Incoming Webhooks in Slack, visit Slack's API documentation.
Usage
Here is a basic example of how to use the Slack Log package to send a message to your Slack channel:
.env 🚨
...
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/******/******/********************"
...
import { slack } from "slack-logs";
or
const { slack } = require('slack-logs');
/*Slack Notification with default format*/
slack.log("Data", [{ title: "1yes!" }]);
slack.log("Data", { title: "2yes!" });
slack.log("Data", "Hello world!");
/*Slack Notification with block format*/
const payload = [
{ title: "Title 1", value: "1234" },
{ title: "Title 2", value: 123 },
{ title: "Title 3", value: { id: 12 } },
{ title: "Title 3", value: [{ id: 12 }] },
];
slack.logBlockMessage("Validation Message!", payload);
/*Slack Notification with colored block format*/
import { LogLevel, slack } from "slack-logs";
const payload = [
{ title: "Title 1", value: "1234" },
{ title: "Title 2", value: 123 },
{ title: "Title 3", value: { id: 12 } },
{ title: "Title 3", value: [{ id: 12 }] },
];
slack.logBlockMessage("Validation Message!", payload);
// Or
slack.logBlockMessage("Validation Message!", payload, LogLevel.DEFAULT);
// Or
slack.logBlockMessage("Validation Message!", payload, LogLevel.ERROR);
// Or
slack.logBlockMessage("Validation Message!", payload, LogLevel.INFO);
// Or
slack.logBlockMessage("Validation Message!", payload, LogLevel.SUCCESS);
// Or
slack.logBlockMessage("Validation Message!", payload, LogLevel.WARN);
Sample code with output:
Sample 1:
import { slack } from "slack-logs";.
slack.log("Data Log with bold", "Here is *BOLD* message");
slack.log("Highlight Log", "`Message`");
slack.log("Emoji :rocket: Log", "Yeah :female-technologist::skin-tone-2:");
slack.log("Object Log", {id:"123", value:"Lorem Impulse"});
Sample 2:
import { LogLevel, slack } from "slack-logs";.
const payload = [
{ title: "Title 1", value: "1234" },
{ title: "Title 2", value: 123 },
{ title: "Title 3", value: { id: 12 } },
{ title: "Title 4", value: [{ id: 12 }] },
];
slack.logBlockMessage("Custom Logs!", payload);
slack.logBlockMessage("Some Information Logs!", payload, LogLevel.INFO);
slack.logBlockMessage("Critical Alert!", payload, LogLevel.ERROR);
Sample 3:
import { LogLevel, slack } from "slack-logs";.
const payload = [
{ title: "Event Name", value: "directMessage" },
{
title: "`to_user` Validation",
value: "Message 'to_user' is required! Current value is null",
},
];
slack.logBlockMessage(`Validation failure on "development" server`, payload, LogLevel.WARN);
Sample 3:
import { LogLevel, slack } from "slack-logs";.
const title =
":rotating_light: Error processing message failure on 'local' server :rotating_light:";
const payload = [
{ title: "Event Name", value: "directMessage" },
{ title: "`error`", value: {} },
];
slack.logBlockMessage(title, payload, LogLevel.ERROR);
Contributing
Contributions are welcome! If you have a feature request, bug report, or a pull request, please open an issue or submit a PR on the GitHub repository
License:
This package is licensed under the MIT License - see the LICENSE file for details.