Last active
December 11, 2015 11:29
-
-
Save bengourley/4594230 to your computer and use it in GitHub Desktop.
Logger boilerplate, where does it go?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Emitter = require('events').EventEmitter | |
function task(options, cb) { | |
var emitter = new Emitter() | |
emitter.emit('log', 'Starting stuff', 'info') | |
emitter.emit('log', 'Fine grained detail about stuff', 'debug') | |
cb(null) | |
return emitter | |
} | |
// | |
// Usage: | |
// | |
task({ .. options .. }, function (err) { | |
// Done | |
}) | |
.on('log', function (msg, level) { | |
// Sanitise level to make sure it is | |
// in 'debug' 'info' 'warn' or 'error' | |
pliers.logger[level](msg) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var defaultLogger = | |
{ debug: console.log | |
, info: console.log | |
, warn: console.log | |
, error: console.log | |
} | |
function task(options, cb) { | |
var logger = options.logger || defaultLogger | |
logger.info('Doing some stuff') | |
// do some stuff | |
logger.debug('Granular detail about the stuff') | |
logger.info('Done doing stuff') | |
cb(null) | |
} | |
// | |
// Usage: | |
// | |
task({ logger: pliers.logger, .. options .. }, function (err) { | |
// done | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment