1.0.2 • Published 4 months ago

simple-slack-logger v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Slack Logger

A simple TypeScript npm package that makes it possible to log messages directly to a Slack channel from any Node.js application.

Installation

npm install simple-slack-logger

Features

  • Console.log compatible API: Works with variable number of arguments just like console.log
  • TypeScript: Full TypeScript support with type definitions
  • Configuration via options: Webhook URL and other settings can be customized as needed
  • Formatted Slack messages: JSON data is nicely formatted in Slack with code blocks
  • Minimal code: Focus on keeping the code as simple as possible

General Usage

// Import the logger
const createSlackLogger = require('simple-slack-logger');

// Create a logger instance
const log = createSlackLogger({
  webhookUrl: 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL',
  source: 'my-application',
  username: 'Logger Bot'
});

// Use exactly like console.log
log('Hello world');
log('User data', { id: 123, name: 'John Doe', email: 'john@example.com' });
log('Multiple', 'arguments', 'work', 'too');

Usage in Auth0 Action

// Import the logger
const createSlackLogger = require('simple-slack-logger');

/**
 * Auth0 login action
 */
exports.onExecutePostLogin = async (event, api) => {
  // Create logger
  const log = createSlackLogger({
    webhookUrl: event.secrets.SLACK_WEBHOOK_URL,
    source: 'auth0-login',
    username: 'Auth0 Bot'
  });
  
  // Use exactly like console.log
  log('User login', event.user.email);
  log('Connection used', event.connection.name);
  log('Complete event data', event);
  
  // Your normal login logic here...
};

Configuration Options

OptionTypeRequiredDefaultDescription
webhookUrlstringYes-Slack Webhook URL
channelstringNoFrom webhookSlack channel to post to
usernamestringNo"Slack Logger"Bot name shown in Slack
iconEmojistringNo":memo:"Emoji icon for bot
sourcestringNo"nodejs-app"Log source identifier
consoleOutputbooleanNotrueWhether to also log to console

Setting up Slack Webhook

  1. Create Slack App

  2. Enable Incoming Webhooks

    • In the left menu, select "Incoming Webhooks"
    • Switch "Activate Incoming Webhooks" to On
    • Click on "Add New Webhook to Workspace"
    • Select the channel you want to send messages to
  3. Copy Webhook URL

    • After authorization, you will see your new webhook URL
    • Copy this URL for use in your application

Development

To build the package locally:

npm install
npm run build

To run tests:

npm test

License

MIT

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago