Library to allow easy userland-changeable logging for node modules/applications
npm install modulelogA library that allows easy userland swapping of different logging libraries.
By default it uses the built-in util.debuglog but any npm module can be
required and used, provided it has at least some of warn, info, error,debug methods that it exposes on its exports. console can also be used,
except that no debug methods will be logged.
``JS`
var log = require('modulelog')('mymodulename');
log.setClass('console');
function cb(err) {
log.error("An error occured", {error: err});
}
A common use case would be to use flags
and allow the user to pass the class name via --logger:
`JS`
var log = require('modulelog')('mymodulename'),
flags = require('flags');
flags.defineString('logger', 'debug', 'Your name');
flags.parse();
log.setClass(flags.get('logger'));
Note: you must pass your module/app name to the result of
require('modulelog') in order to get an instance to log with. The actualdebuglog
class and level is globally controlled across modules but each module
must have a name for the prefix.
Change the default logging library to be name. Built-in values are default,debuglog, console. If an empty string is passed in, all logs will berequire
discarded. Any non-built-in values will be passed to .
Calls setLevel on the backing library. If the library doesn't supportdebuglog
different levels (like or console) then this does nothing.
Calls the corresponding method on the backing library. It is recommended
that you call with message and an object of extra data, however any number
of arguments are accepted and passed on.
Calls the corresponding method on the backing library. If the library has no
fatal method, then error is called instead. After logging, process.exit
is invoked.
If you're trying to set the level or class in a static fashion setLevel andsetClass` are both exposed statically on the exports for the module.