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 = require('fs');
var logger = require('oniyi-logger')('my-awesome-module');
logger.info('my %s message', 'info');
logger.debug('my debug message');
logger.warn('my warn message');
logger.error('my error message');
var labeledFileLog = require('oniyi-logger')('file', {sink: fs.createWriteStream('file.log, {flags: 'a'}')});
labeledFileLog.info('my info message');
labeledFileLog.debug('my debug message');
labeledFileLog.warn('my warn message');
labeledFileLog.error('my error message');
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