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

slack-logger

v2.5.0

Published

Bunyan-compatible logger that send pretty formatted messages to a Slack channel.

Downloads

189

Readme

Slack logger nodejs library

Coverage Downloads Version License

Sends pretty formatted messages to a Slack channel.

Example

Installation

This package is distributed via npm

npm install slack-logger

Features

  • Logs messages to a Slack channel.
  • Configurable logging level.
  • Pretty prints additional information.
  • Support handling messages from the channel.
  • Has built-in message handler for setting logging level.
  • One can easily add custom message handlers for restarting the server, deploying a new version etc.
  • Provides optional console logger that outputs nicely formatted color-coded log info to console.
  • Can integrate with any logging solution.
  • Works great with Bunyan but not required.
  • Also plays nicely with Winston.
  • Written in TypeScript.
  • Includes 100% test coverage.

Using in your application

Recommended way of using logging and the slack logger in your application is by using the provided Logger class that's a simple abstraction on top of Bunyan for getting component-specific loggers. See the examples/4-example-logger.ts for details.

import logger from "./services/logger";

// use the logger.get(componentName, filename) in each file to log for a specific component
const log = logger.get("register", __filename);

// logging message and info separately is recommended as the info gets nicely formatted and message becomes searchable
log.info(
  {
    user: {
      name: "Jack Daniels",
      email: "[email protected]",
    },
  },
  "user was successfully registered",
);

Examples

Take a look at the examples folder. It shows how to use the library for custom manual integrations, using it with Bunyan and Winston and also using the provided Logger class that uses Bunyan.

The examples can be run with npm start. They use a .env file for configuration, a .env-example file is provided (just copy it to .env file and change accordingly). Alternatively is the configuration file does not exist, the npm start script will ask for the details in the console and generates the configuration file.

Adding custom message handlers

Responding to slack channel messages is as easy as registering a simple handler.

// example of adding a custom message handler
slackLog.addMessageHandler({
  getName: () => "restart",
  getDescription: () => "restarts the application",
  handleMessage: (_message, log) => {
    log.post("restarting the application..");

    restart(); // implement this in some way
  },
});

Using the console logger

An optional console logger for Bunyan is provided that outputs the same information formatted nicely for the console.

Console log

To use this, simply register instance of ConsoleLog as a Bunyan stream.

import { ConsoleLog } from "slack-logger";

// register the console logger
logger.addStream({
  name: "console",
  level: "info",
  type: "raw",
  stream: new ConsoleLog({
    basePath: path.join(__dirname, "..", ".."), // configure to point to the root of the project
  }),
});

Commands

  • npm start to run the examples (prompts for configuration and choice of example).
  • npm build to build the production version.
  • npm test to run tests without code coverage.
  • npm run coverage to run tests with code coverage.
  • npm run lint to lint the codebase.
  • npm run prettier to run prettier.
  • npm run audit to run all pre-commit checks (prettier, build, lint, test). Run this before pull requests.