1.0.2 • Published 4 months ago
simple-slack-logger v1.0.2
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
Option | Type | Required | Default | Description |
---|---|---|---|---|
webhookUrl | string | Yes | - | Slack Webhook URL |
channel | string | No | From webhook | Slack channel to post to |
username | string | No | "Slack Logger" | Bot name shown in Slack |
iconEmoji | string | No | ":memo:" | Emoji icon for bot |
source | string | No | "nodejs-app" | Log source identifier |
consoleOutput | boolean | No | true | Whether to also log to console |
Setting up Slack Webhook
Create Slack App
- Go to https://api.slack.com/apps
- Click on "Create New App"
- Choose "From scratch" and give your app a name
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
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