npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

winston-transport-discord

v1.0.3

Published

Winston logging transport for emitting log entries to a Discord channel.

Downloads

185

Readme

winston-transport-discord

npm version   Tests   Release

Winston logging transport for emitting log entries to a Discord channel.

Compatibility

  • Node 16+
  • Winston 3

Installation

# npm
npm install winston-transport-discord

# yarn
yarn add winston-transport-discord

Usage

JavaScript

const winston = require( 'winston' );
const DiscordTransport = require( 'winston-transport-discord' );

// Create the transport.
const transport = new DiscordTransport({
    discord: {
        webhook: {
            url: 'https://discord.com/api/webhooks/123456789012345678/AbCDefGhIJKLmnOpqRStUVwXyZaBCDefGHiJK_lMnoPQRsTuVWXyZAbcDefGHiJKlmnO'
        }
    }
});

// Create the logger.
const logger = winston.createLogger({
    transports: [ transport ]
});

// Use it w/ full options.
logger.log( 'info', 'This is the log message.' );

// Use it w/ helper methods.
logger.info( 'This is the log message.' );

Typescript

import winston from 'winston';
import DiscordTransport, { DiscordTransportOptions } from 'winston-transport-discord';

// Create options to illustrate typing.
const options: DiscordTransportOptions = {
    discord: {
        webhook: {
            url: 'https://discord.com/api/webhooks/123456789012345678/AbCDefGhIJKLmnOpqRStUVwXyZaBCDefGHiJK_lMnoPQRsTuVWXyZAbcDefGHiJKlmnO'
        }
    }
};

// Create the transport.
const transport = new DiscordTransport( options );

// Create the logger.
const logger = winston.createLogger({
    transports: [ transport ]
});

// Use it w/ full options.
logger.log( 'info', 'This is the log message.' );

// Use it w/ helper methods.
logger.info( 'This is the log message.' );

Handler Configuration

Both bot- and webhook-based log handling is supported, though webhooks are preferred due to their simplicity for Discord server administrators as well as this library.

Webhooks (Preferred)

Create a Webhook

  1. Open Discord.
  2. Navigate to the Discord server in which log entries will be sent.
  3. Open the server settings, and click "Integrations".
  4. Click "New Webhook", and provide:
    • Avatar: Optionally upload an avatar image to display with log messages.
    • Name: Webhook name as it will appear as the author of log messages.
    • Channel: Text channel in which log messages will be sent.
  5. Click "Save Changes".
  6. Click "Copy webhook URL", and keep it handy.

Webhook Options

The webhook handler can be configured in one of two ways:

Explicitly in transport options:

const winston = require( 'winston' );
const DiscordTransport = require( 'winston-transport-discord' );

// Create the transport.
const transport = new DiscordTransport({
    discord: {
        webhook: {
            url: 'PASTE THE WEBHOOK URL HERE'
        }
    }
});

// ...

Environment variables:

Using a package like dotenv or some other means of configuring a Node environment variable, set the value of DISCORD_LOGGING_WEBHOOK_URL to the webhook URL. No other options are needed.

# .env File
DISCORD_LOGGING_WEBHOOK_URL="PASTE THE WEBHOOK URL HERE"
const dotenv = require( 'dotenv' );
const winston = require( 'winston' );
const DiscordTransport = require( 'winston-transport-discord' );

// Run this before anything else.
dotenv.config();

// Create the transport.
const transport = new DiscordTransport();

// ...

Bots

Prior to using this handler it's necessary to create a bot application in Discord, and invite that bot to your Discord server. From the bot application's page in the Discord Developer Portal you'll need to make note of the token generated in the Bot Settings page.

You'll also need to acquire a channel ID from the text channel in your Discord server in which you want the logs to be sent.

Acquire a Channel ID

  1. Open Discord.
  2. Click the settings cog icon in the bottom-left corner.
  3. Click "Advanced" beneath "App Settings" on the left of the settings view.
  4. Ensure that "Developer Mode" is enabled.
  5. Close Settings, and navigate to the Discord server in which log entries will be sent.
  6. Find the desired text channel in the channel list, right-click it, and select "Copy ID" at the bottom of the context menu.

Bot Options

The bot handler can be configured in one of two ways:

Explicitly in transport options:

const winston = require( 'winston' );
const DiscordTransport = require( 'winston-transport-discord' );

// Create the transport.
const transport = new DiscordTransport({
    discord: {
        bot: {
            channel: 'PASTE THE CHANNEL ID HERE',
            token: 'PASTE THE BOT TOKEN HERE'
        }
    }
});

// ...

Environment variables:

Using a package like dotenv or some other means of configuring a Node environment variable, set the value of DISCORD_LOGGING_BOT_CHANNEL to the channel ID and the value of DISCORD_LOGGING_BOT_TOKEN to the bot token. No other options are needed.

# .env File
DISCORD_LOGGING_BOT_CHANNEL="PASTE THE CHANNEL ID HERE"
DISCORD_LOGGING_BOT_TOKEN="PASTE THE BOT TOKEN HERE"
const dotenv = require( 'dotenv' );
const winston = require( 'winston' );
const DiscordTransport = require( 'winston-transport-discord' );

// Run this before anything else.
dotenv.config();

// Create the transport.
const transport = new DiscordTransport();

// ...

Metadata and Error Logging

Metadata

Log messages will always contain metadata fields of host, level, and timestamp. You may also provide additional metadata at both the global and log message levels as well. Both expect simple key-value string pairs.

Global Metadata

Global metadata - included with every log entry - is set in the transport options. This example adds service name and author:

const winston = require( 'winston' );
const DiscordTransport = require( 'winston-transport-discord' );

// Create the transport.
const transport = new DiscordTransport({
    metadata: {
        service: 'My Application',
        author: '<@some-discord-id>'
    }
});

// ...

Entry Metadata

Entry metadata is only included with its parent log entry. This example adds class and function name:

// ...Winston already setup with transport.

// Dummy metadata.
const meta = {
    class: 'MyClass',
    function: 'someFunction'
};

// Use it w/ full options.
logger.log( 'info', 'This is the log message.', { meta });

// Use it w/ helper methods.
logger.info( 'This is the log message.', { meta });

Error Logging

If an error has been sent with a log entry it will be shown as the description of the log message embed automatically, but only if the log level is error. For example:

// ...Winston already setup with transport.

// Dummy error.
const error = new Error( 'Uh oh!' );

// Use it w/ full options.
logger.log( 'error', 'Something bad happened.', error );

// Use it w/ helper methods.
logger.error( 'Something bad happened.', error );

Example Output

Info

Info

Error

Error