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

@luckbox/logger-factory

v4.0.0

Published

Easy to use logger with several levels of logging as well as different adapters that can be used separately or in combinations

Downloads

53

Readme

Build Status

Coverage Status

Logger Factory

This logger factory produces Logger instances that can have different prefixes, but all share the same logging level that is set for the LoggerFactory instance. Note that logging level is configured per adapter, i.e. you can be more verbose with one adapter and less verbose for another.

Usage

The following example creates a LoggerFactory instance, using only a console adapter with log level info, and that instance is used to create two Logger instances with Classname and Classname2 prefixes respectively.

const LoggerFactory = require('./LoggerFactory');

const infoLoggerFactory = new LoggerFactory({
    adapters: [{
        name: Adapters.Console,
        config: {
            logLevel: 'info'
        }
    }]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');

The following example creates a LoggerFactory instance, using only two adapters - console and sentry, with log levels info and warn respectively, meaning that all messages above info will only be logged via the Console adapter, while warn and above will also be logged via Sentry adapter. After that, similarly to our previous example, that LoggerFactory instance is used to create two Logger instances with Classname and Classname2 prefixes respectively.

const LoggerFactory = require('./LoggerFactory');

const infoLoggerFactory = new LoggerFactory({
    adapters: [
        {
            name: Adapters.Console,
            config: {
                logLevel: 'info'
            }
        },
        {
            name: Adapters.Sentry,
            config: {
              logLevel: 'warn',
              dsn: 'https://[email protected]/6',
              environment: 'testing',
            }
        }
    ]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');

LoggerFactory methods

  • constructor ({ adapters }) - Creates an instance LoggerFactory instance with the requested array of adapters. List of the supported adapters, along with their configuration requirements, follows below.
  • create(prefix?: string) - Creates an instance of the logger, using already specified adapters.

Adapters

The following is a list of all supported adapters, along wit an example usage of each of them. The possible logging levels are the same for each of the adapters and are listed below this list.

Default configuration (works for all adapters):

  • skipTimestamps Whether to include the timestamps in the logged message. Can be turned off if using kubernetes, since it has integrated functionality to timestamp all messages. Defaults to false.
  • logLevel - The minimum logging level that will be logged with that adapter.

Console adapter

Console adapter has no additional configuration.

const LoggerFactory = require('./LoggerFactory');

const infoLoggerFactory = new LoggerFactory({
    adapters: [{
        name: Adapters.Console,
        config: {
            logLevel: 'info'
        }
    }]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');

Sentry adapter

Sentry has the following additional configuration settings:

  • dsn - Client key, used by Sentry to determine where to send the event to
  • environment - Used to separate errors from different environments
  • debug - Whether to enable Sentry adapter in debug mode (NOTE: even in debug mode, not working Sentry server will not crash the code). Defaults to false.
const LoggerFactory = require('./LoggerFactory');

const infoLoggerFactory = new LoggerFactory({
    adapters: [{
        name: Adapters.Sentry,
        config: {
          logLevel: 'warn',
          dsn: 'https://[email protected]/6',
          environment: 'testing',
          debug: false,
        }
    }]
});
const infoLogger = infoLoggerFactory.create('Classname');
const anotherInfoLogger = infoLoggerFactory.create('Classname2');

Log levels

The following log levels are supported, listed from least verbose to the most verbose:

  • off - Nothing is logged
  • system - System messages (used via .system()) are logged
  • error - Error messages (used via .error()) are logged
  • warn - Warn messages (used via .warn()) are logged
  • info - Info messages (used via .info()) are logged
  • debug - Debug messages (used via .debug()) are logged

Note that in all cases, when setting a value, all less-verbose types of messages are also logged, for example if you set warn as log level, off, system and error are also logged.

Logger Methods

The following methods can be called on a Logger instance:

  • system(message) - The provided message is logged via console.log if the configured log level allows
  • error(message) - The provided message is logged via console.error if the configured log level allows
  • warn(message) - The provided message is logged via console.warn if the configured log level allows
  • info(message) - The provided message is logged via console.info if the configured log level allows
  • debug(message) - The provided message is logged via console.log if the configured log level allows
  • clear - Console is cleared