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

log-chainable

v0.2.2

Published

A JS log wrapper that makes defining namespaces easy with method chaining

Downloads

11

Readme

log-chainable

A utility for organizing javascript log statements into namespaces. Why? Because reading pages of random logs sucks, and you're too lazy/smart to annotate each statement.

Installation

npm install log-chainable

Basic Usage

The most basic usage:

const log = require('log-chainable');

log('hi mom'); // [info] hi mom
log.warn('bye mom'); // [warn] hi mom

Using namespaces:

const log = require('log-chainable');

const logFamily = log.namespace('greetings').namespace('family');

logFamily('hi mom'); // [info] (greetings.family) hi mom

Namespaces are smart:

// greeter.js

const log = require('log-chainable');

const logGreeter = log.namespace('myApp', 'utils', module); 
logGreeter('greetings'); // [info] (myApp.utils.greeter) greetings

Be concise:

// greeter.js

const log = require('log-chainable').namespace('myApp', 'utils', module);

class MyClass {
    constructor () {
        this.log = log.namespace(this);
    }

    doThatThing () {
        this.log('done doing that thing');
    }
}

const myInstance = new MyClass();
myInstance.doThatThing() // [info] (myApp.utils.greeter.MyClass) done doing that thing

Handlers

Each namespace can have it's own handler:

const log = require('log-chainable');

log.namespace('myApp', 'mySpecialModule')
    .handler(function myHandler (level, nameStack, args) {
        process.stdout.write(`myHandler: ${ level } ${ nameStack.join('/') } ${ args.join(', ') }\n`);
    });

log.namespace('myApp')('one', 'two'); // [info] (myApp) hi mom
log.namespace('myApp', 'mySpecialModule')('one', 'two'); // myHandler: info myApp/mySpecialModule one, two

Handlers cascade to child namespaces:

const log = require('log-chainable');

log.namespace('myApp', 'mySpecialModule')
    .handler(function myHandler (level, nameStack, args) {
        process.stdout.write(`myHandler: ${ level } ${ nameStack.join('/') } ${ args.join(', ') }\n`);
    });

log.namespace('myApp')('one', 'two'); // [info] (myApp) hi mom
log.namespace('myApp', 'mySpecialModule')('one', 'two'); // myHandler: info myApp/mySpecialModule one, two
log.namespace('myApp', 'mySpecialModule', 'mySpecialChildModule')('three', 'four'); // myHandler: info myApp/mySpecialModule/mySpecialChildModule three, four

log-chainable comes with some handlers included:

const log = require('log-chainable');
const handlers = require('log-chainable/handlers');

const myAppLogger = log.namespace('myApp').handler(handlers.default);

myAppLogger
    .namespace('colorfulModule')
    .handler(handlers.minimalConsoleColorized);

myAppLogger
    .namespace('dullModule')
    .handler(handlers.minimalConsole);

The Default Handler

The default handler is a minimalist call to console.log with some extra colorization.

The default handler will hide .debug() calls unless the DEBUG environment variable is set.

const log = require('log-chainable');
const handlers = require('log-chainable/handlers');

// this equivalent is already set for you
log.handler(handlers.default)

process.env['DEBUG'] = undefined;
log.debug('beep beep beep'); // <nothing happens>

process.env['DEBUG'] = 'true';
log.debug('beep beep beep'); // [debug] beep beep beep

List of Available Handlers

  • handlers.minimalConsoleColorized aka the default handler
  • handlers.minimalConsole same as above with no color

Feel free to contribute more. Speaking of ...

Contributing

Just please run the tests and linter.

npm run check