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

bpd-logger

v1.0.3

Published

Simple logger implementation with support for plugins

Downloads

9

Readme

bpd-logger

Simple logger implementation with support for plugins.

Logging levels

Library supports following logging levels

  • none - (default) - nothing is reported
  • exception - only exceptions are reported (Error objects)
  • error - reports error messages and exceptions
  • warn - reports warning and error messages and exceptions
  • info - reports messages, warnings, errors and exceptions
  • debug - reports all messages

Reporters

Reporters are needed to post log messages to some destination - it can be anything from console to API endpoint. Library provides simple implementation of console reporter.

Object passed to reporter is an instance of Log which specify message log level, log properties, date and optionally error object

Example reporter:

async function LogReporter(log: Log) {
	console.log(log);
	return true;
}

Extensions

Extensions are callbacks that are executed right before message is logged. They return property name and it's value which is attached to props map in Log object.

Example extension:

let count = 0;
function SampleExtension() {
	count += 1;
	return ["counter", count];
}

Usage

Logger gathers fields set as props and pushes them along with message to reporters when user invokes one of log messages. Properties can be added via method prop, special methods like id, module,method or by extensions (extension callbacks are executed every time before pushing log message to reporters).

Configuration

With minimal effort you have to specify at least one reporter and logging level because by default logging level is set to none. Logger can be configured via method configure. It returns builder which allows to prepare setup. Builder exposes following methods:

  • withLevel(level: LogLevel) - sets logging level
  • withReporters(...reporters: LoggerReporter[]) - adds reporters to the list
  • withExtensions(...extensions: LoggerExtension[]) - adds extension to the list
  • clearReporters() - clears reporters list
  • clearExtensions() - clears extensions list
  • save() - spawns new logger instance with updated setup

Methods

Logger exposes several methods:

  • id(id: string) - sets property id
  • module(method: string) - sets property module
  • method(method: string) - sets property method
  • prop(key: string, value: LogProperty) - sets any property by kay and value

NOTE

All of above methods return new instance of logger extended by property

  • configure(setup?: LoggerSetup) - returns object that lets to configure setup of logger. Object takes copy of logger props, so instance returned by configuration builder will get all data that has been filled before. More information can be found in section dedicated to configuration builder.

  • debug(message: string),info(message: string),warn(message: string),error(message: string) - all methods are dedicated to logging a message. They fill proper log level and property message, then push it to reporter

  • exception(e: Error | string) - specific method that accepts Error object. It also pushes log to reporters along with message and error

  • log(level: LogLevel, message?: string, error?: Error) - common method for logging.

NOTE

Above methods return logging result

  • measure(callback, options?) - method measures callback execution time. It returns execution result (not a logging result) or null if method failed to execute. Measured result is transformed to a log. Method fills properties message and performance. It also logs out an exception log if case of callback failure. Method accepts functions and promises as callback property. Options are optional and may specify a label (default is Performance) displayed in log message, fraction which specifies fraction digit in method toFixed called on execution time, rethrow which specifies whether error thrown by callback shall be rethrown (instead of returning result of null).