A simple loglevel and label wrapper around process.stdout
Install
$ npm install --save oniyi-logger
Usage
all log functions work similar to console.log() and can take multiple arguments in a printf()-like way. Note that the debug()
method is a noop per default. To enable debug()
logging, you must use a labled logger, and list the label in the NODE_DEBUG
environment variable. NODE_DEBUG
must be a comma,
or space separated list
var fs = ; // standard use-case, will log to process.stdoutvar logger = 'my-awesome-module'; logger;// INFO [my-awesome-module] my info messagelogger;// Does not log anythinglogger;// WARN [my-awesome-module] my warn messagelogger;// ERROR [my-awesome-module] my error message // log to a filevar labeledFileLog = 'file' sink: fs; labeledFileLog;// writes "INFO [file] my info message" to file.loglabeledFileLog;// writes does not write anything to file.loglabeledFileLog;// writes "WARN [file] my warn message" to file.loglabeledFileLog;// writes "ERROR [file] my wrror message" to file.log
You can use nested lables to controle debug messages with finer granularity.
so with process.env.NODE_DEBUG = 'foo:bar'
, you get this:
const oniyiLogger = require('oniyi-logger');
const fooLogger = oniyiLogger('foo');
const barLogger = oniyiLogger('foo:bar');
fooLogger.debug('my debug message');
// Does not log anything
barLogger.debug('my debug message');
// DEBUG [foo:bar] my debug message
and with process.env.NODE_DEBUG = 'foo:*'
, you get this:
const oniyiLogger = require('oniyi-logger');
const fooLogger = oniyiLogger('foo');
const barLogger = oniyiLogger('foo:bar');
fooLogger.debug('my debug message');
// DEBUG [foo] my debug message
barLogger.debug('my debug message');
// DEBUG [foo:bar] my debug message
License
Apache 2.0 © Benjamin Kroeger