1.2.5 • Published 3 years ago

@darkobits/log v1.2.5

Weekly downloads
105
License
WTFPL
Repository
github
Last release
3 years ago

log

A thin wrapper around the wonderful npmlog that creates unique instances.

Installation

$ npm install --save @darkobits/log

Usage

This package's default export is a factory function with the following signature:

ParameterTypeDescription
headingstring(Optional) Log heading to set.
levelstring(Optional) Log level to set.

Log level will be set to process.env.LOG_LEVEL if set. Otherwise, it will use the npmlog default level, info.

Example:

foo.js

import LogFactory from '@darkobits/log';

// For convenience, you can set the heading and/or level via the factory function.
const log = LogFactory('foo', 'silly');

export default function init() {
  log.silly('init', 'Hello, there!');
}

Using npmlog alone, bar.js below would wind up importing the same object imported by foo.js, with its heading and level already set. Even worse, if bar.js changes them and then foo.js logs something, the resulting output will be completely hosed.

bar.js

import LogFactory from '@darkobits/log';

const log = LogFactory();

// You may also set the heading via the 'heading' property, per usual.
log.heading = 'bar';

export default function barnacles() {
  log.info('barnacles', 'Aw, shucks!');
}

With this setup, we can now do the following:

baz.js

import init from './foo';
import barnacles from './bar';

barnacles();
init();

And get the following output:

bar foo

Why?

npmlog is great, but it was designed to be used by one package at a time. When there are multiple packages that depend on npmlog in the same execution context, things get wonky rather quickly. This package guarantees that each import gets its own instance with its own state that it can customize as it sees fit.