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

@eqworks/node-slack-logger

v0.2.0

Published

Log events to slack channels

Downloads

7

Readme

Node Slack Loger

NPM version

Node Slack Logger is a utility to log messages or errors to a Slack channel. It is built around a simple function, getLogger, which can be imported in any module in order to access the desired logger, waiving the need to pass around logger objects.

Installation

yarn add @eqworks/node-slack-logger
OR
npm install @eqworks/node-slack-logger

Interface

  1. function getLogger
  • getLogger(id, config) - Instantiates/updates logger at id with config and returns said logger.
  • getLogger(config) - Instantiates/updates the default logger with config and returns said logger.
  • getLogger(id) - Returns logger at id or throws an error if the logger was not previously instantiated.
  • getLogger() - Returns the default logger or throws an error if the logger was not previously instantiated.
  1. Logger config
  • send - Function - Callback to invoke in order to post to the Slack API
  • format - Number | string | function, optional, defaults to 1 (SLACK) - API payload formatter
  • appName - String, optional, defaults to My App
  • minLevel - Number | string, optional, defaults to 3 (WARNING) - Minimum severity which must be achieved for an event to be logged
  • colors: - Object, optional - Map of log levels (number) to colours (string)
  1. Logger instance
  • setConfig(config) - Updates the logger's config
  • log(level, error, context?) - Logs an error to Slack at the given severity level.
  • log(level, message, context?) - Logs a message to Slack at the given severity level.
  • log(level, options, context?) - Logs a message with options to Slack at the given severity level.
  • debug(error | message | options, context?) - Logs with level DEBUG
  • info(error | message | options, context?) - Logs with level INFO
  • warning(error | message | options, context?) - Logs with level WARNING
  • error(error | message | options, context?) - Logs with level ERROR
  • critical(error | message | options, context?) - Logs with level CRITICAL
  1. Log event options
  • message - String
  • stack - String, optional - Error stack
  • context - Object, optional - Context object, e.g. { userName: 'paul1234' }

Usage

const { getLogger, logLevels } = require('@eqworks/node-slack-logger')

// api - using axios
const axios = require('axios')
const send = (body) => axios.post(
  'https://hooks.slack.com/...',
  body,
  {
    timeout: 5000, // 5 seconds
    maxRedirects: 0,
  },
)

// api - using Slack sdk
const { IncomingWebhook } = require('@slack/webhook')
const webhook = new IncomingWebhook('https://hooks.slack.com/...')
const send = webhook.send.bind(webhook)

// instantiate default logger
const logger = getLogger({
  send,
  appName: 'My Fantastically Magical App',
  minLevel: logLevels.WARNING,
})

// Express.js middleware
const errorHandler = (err, req, res, next) {
  logger.error(err, req.userContext)
}

// Logger can also be accessed by calling getLogger in any module - provided that it was previously instantiated
someJob.then(() => getLogger().info('Job ran successfully')).catch((err) => getLogger().error(err))

// Using multiple loggers
getLogger('abc', {
  send: ...,
  appName: 'ABC not the Alphabet Song',
  minLevel: logLevels.INFO,
})

getLogger('xyz', {
  send: ...,
  appName: 'XYZ more than a TLD',
  minLevel: logLevels.INFO,
})

function authentication(user, product) {
  try {
    // do something
  } catch (err) {
    // assuming product is one of 'abc' or 'xyz'
    getLogger(product).warning(err, { region: user.region })
  }
}