UNPKG

slack-logs

Version:

slack-logs

177 lines (126 loc) 4.77 kB
## 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.](https://api.slack.com/messaging/webhooks) ### 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 for sending logs and notifications. # Replace with your actual webhook URL. SLACK_WEBHOOK_URL="https://hooks.slack.com/services/******/******/******************** # Optional field, defaults to "true". # Set to "false" to disable Slack logs globally without changing the code. ENABLE_SLACK_LOGS=true # Note: No need to define this variable unless you want to disable Slack logs. # If variable is not defined, it defaults to "true". ... ``` ``` 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 Image](https://i.imgur.com/ucAnAiJ.png) #### 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 Image](https://i.imgur.com/Ohlktzr.png) #### 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 Image](https://i.imgur.com/yncdwGJ.png) #### 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); ``` ![Sample Image](https://i.imgur.com/XszvEEw.png) ### 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.