Created
September 11, 2013 15:20
-
-
Save logicalparadox/6525152 to your computer and use it in GitHub Desktop.
Super simple logger using transmute streams.
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
/*! | |
* Import transmute | |
*/ | |
var transmute = require('transmute'); | |
/*! | |
* Base logger | |
*/ | |
var log = transmute({ | |
options: { objectMode: true, highWaterMark: 1 } | |
, transform: function(obj, enc, cb) { | |
obj.timestamp = new Date(); | |
cb(null, obj); | |
} | |
, flush: function(cb) { | |
var ended = {}; | |
ended.timestamp = new Date(); | |
ended.type = 'info'; | |
ended.msg = 'Log ended'; | |
this.push(ended); | |
cb(); | |
} | |
}); | |
/*! | |
* Debug filter | |
*/ | |
var nodebug = transmute({ | |
options: { objectMode: true, highWaterMark: 1 } | |
, transform: function(obj, enc, cb) { | |
if (obj.type === 'debug') return cb(); | |
cb(null, obj); | |
} | |
}); | |
/*! | |
* Display stream | |
*/ | |
var display = transmute({ | |
writable: { objectMode: true, highWaterMark: 1 } | |
, transform: function(obj, enc, cb) { | |
cb(null, obj.timestamp.toUTCString() + ' [' + obj.type + '] ' + obj.msg + '\n'); | |
} | |
}); | |
/*! | |
* Plumbing | |
*/ | |
log.pipe(nodebug).pipe(display).pipe(process.stdout); | |
/*! | |
* Log stuff | |
*/ | |
log.write({ type: 'info', msg: 'hello universe' }); | |
log.write({ type: 'debug', msg: 'debug the universe' }); | |
log.write({ type: 'info', msg: 'did you see the debug, thought not.' }); | |
log.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment