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

leo-winston

v0.0.1

Published

Hierarchical winston loggers

Downloads

1

Readme

Leo-Winston

Hierarchical logger system for winston with configurable data flows, inspired by Python logging facility.

Usage

Container

First, you instantiate the LeoWinston container object: the service object which allows you to access all loggers you create with it.

var LeoWinston = require('leo-winston').LeoWinston;

var leo = new LeoWinston({
    levels: { silly: 0, debug: 1, verbose: 2, info: 3, warn: 4, error: 5 },
    decorate: true,
    propagate: true
});

The following LeoWinston container options are available:

  • levels: Log levels to use for all loggers. Default: uses winston's npm levels;

  • decorate: Boolean: Whether to decorate each message with the logger name. Default: true

  • propagate: Boolean: Use log messages propagation?

    When propagation is enabled, messages bubble up to parent loggers

    When a message is logged with the 'a.b.c.d' logger, it will propagate to loggers 'a.b.c', 'a.b', 'a', and finally, 'root'.

Define loggers

You define loggers with the following method:

LeoWinston.add(name, options):Logger

  • name: String: The name of the logger to add.

    In order to use propagation, use '.'-notation to qualify logger names.

  • options: Object?: Logger options object:

    • propagate: Boolean: Whether messages from this logger propagate further. Default: true

      When a logger is created with propagate: false, it will consume the messages without passing them on.

    • transports: Object: Winston transports configuration. See: Working with transports.

It's required that you have a single logger named 'root': this topmost logger catches messages from all registered loggers (unless they explicitly define propagation: false).

leo.add('root', {
    transports: {
        console: { level: 'silly', silent: false, colorize: true, timestamp: true }
    }
});

leo.add('http', { // will pass the messages on to 'root'
    transports: {
        file: { // log HTTP requests to a file
            level: 'silly', silent: false, colorize: false, timestamp: true,
            filename: '/tmp/tmp/root.log', json: false
        }
    }
});

leo.add('http.users', {}); // no logging, just propagate for now

leo.add('http.requests', {
    propagate: false, // don't propagate
    transports: { // log to console
        console: { level: 'silly', silent: false, colorize: true }
    }
});

It's wise to keep logger options in a config file: this way you'll have flexible app configurations.

Logging

After you've configured your loggers, use the following method to get a logger:

LeoWinston.get(name):Logger

If the logger was not defined, it's created as a no-op logger. When propagation is enabled, its messages propagate to the 'root' logger.

Wielding a logger, use it like a normal Winston logger:

var logger = leo.get('http.users');
logger.log('info', 'message'); // (level, message[, meta][, callback])

// Each log level gets a corresponding method
logger.silly('message');
logger.debug('message');
logger.verbose('message');
logger.info('message');
logger.warn('message');
logger.error('message');

Propagation Concepts

  1. When anything is logged to logger 'A.B.C.D', the message propagates up to loggers 'A.B.C', 'A.B', 'A' and 'root'
  2. If any of the intermediate loggers does not exist - no problem: the message proceeds going up through the chain
  3. If any logger is configured with propagation: false, the message propagation stops on it.

Receipts

Handling Uncaught Exceptions

// Assuming your 'root' logger is configured correctly
leo.add('error');

process.on('uncaughtException', function (err) {
    leo.get('error').error(err + "\n" + err.stack);
});