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

@superhero/log

v4.0.0

Published

A simple logger - intended to make it easy to configure, control and extend application log behaviour.

Downloads

98

Readme

Log

A simple logger designed to be a versatile Node.js utility component, offering customizable formatting, colorized output, and event-driven hooks for use cases where logging needs to be pluggable. Designed to create a standard for the general @superhero tool chain, but is an isolated compoent that can be used to seamlessly integrate into any modern Node.js application.

Features

  • Event-driven logging: Emits events (info, warn, fail) for enhanced observability.
  • Customizable output: Includes colored and non-colored output options.
  • Message formatting: Template literal support for dynamic log messages.
  • Global event hooks: Observe logs globally across multiple instances.
  • Mute control: Granular muting for info, warn, and fail logs.

Installation

npm install @superhero/log

Usage

Basic Example

import Log from '@superhero/log'

const log = new Log()

// Default logging
log.info`This is an info message.`    // [LOG] ⇢ This is an info message.
log.warn`This is a warning message.`  // [LOG] ⇢ This is a warning message.
log.fail`This is a failure message.`  // [LOG] ⇢ This is a failure message.

Custom Configuration

const log = new Log({ label: '[MyApp]' })

// Customize label
log.info`Custom label example.` // [MyApp] ⇢ Custom label example.

// Mute specific log types
log.config.muteWarn = true
log.warn`This warning will be muted.` // ... no console output

Observability

Each log type (info, warn, fail) triggers an event both locally and globally, on a static global event emitter, as well as an local instance event emitter.

It's possible to listen to one or both of the event emitters to extend the expected reaction to a log message, for example to perist the log event in a database.

import Log from '@superhero/log'

const log = new Log({ label: '[MyApp]', mute: true })

// Instance Observability
log.on('fail', (config, ...args) => 
{
  config.label              // [MyApp]
  config.mute               // true
  args[1] instanceof Error  // true
})

// Global Observability
Log.global.on('fail', (config, ...args) => 
{
  config.label              // [MyApp]
  config.mute               // true
  args[1] instanceof Error  // true
})

// Prints to the console and triggeres an event to 
// both the static and instance event emitters.
log.fail`Fail message with error: ${new Error()}`

Configuration

| Property | Type | Default | Description | |-----------------|---------|-----------|-----------------------------------------------| | label | String | [LOG] | Prefix for all log messages. | | mute | Boolean | false | Mute all log types. | | muteInfo | Boolean | false | Mute only info logs. | | muteWarn | Boolean | false | Mute only warn logs. | | muteFail | Boolean | false | Mute only fail logs. |

Tests

The library is fully tested using the Node.js native test module.

Run tests:

node test.js

Test Coverage

▶ @superhero/core
  ✔ Info (3.458554ms)
  ✔ Warn (1.109146ms)
  ✔ Fail (1.093924ms)

  ▶ Mute
    ✔ Mute all (0.790623ms)
    ✔ Mute info (0.91809ms)
    ✔ Mute warn (0.288443ms)
    ✔ Mute fail (0.707929ms)
  ✔ Mute (5.274307ms)

  ✔ Observe log info (0.972545ms)
  ✔ Observe log warn (0.636159ms)
  ✔ Observe log fail (0.355621ms)
  ✔ Distinguish types in observed log messages (2.140068ms)
  ✔ Distinguish types in observed global log messages (0.664552ms)
  ✔ Colored controlled format (0.741308ms)
  ✔ Supress (0.486298ms)
✔ @superhero/core (19.472827ms)

tests 15
suites 1
pass 15

----------------------------------------------------------------
file            | line % | branch % | funcs % | uncovered lines
----------------------------------------------------------------
index.js        | 100.00 |   100.00 |  100.00 | 
index.test.js   | 100.00 |   100.00 |  100.00 | 
----------------------------------------------------------------
all files       | 100.00 |   100.00 |  100.00 | 
----------------------------------------------------------------

License

This project is licensed under the MIT License.

Contributing

Feel free to submit issues or pull requests for improvements or additional features.