A wrapper around the wonderful npmlog
that allows for the creation of unique instances.
Install
$ npm i @darkobits/log
Use
This package's default export is a factory function with the following signature:
LogFactory(heading: string, level?: string);
If level
is omitted, the 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:
Now With 50% More Chalk!
This package comes with chalk
included, available at log.chalk
:
log.info('doStuff', `We did the ${log.chalk.green.bold('stuff')}, huzzah!`);
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.