Created
September 6, 2013 19:23
-
-
Save cowboy/6468672 to your computer and use it in GitHub Desktop.
Muxing logger #2
This file contains 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 EventEmitter = require('events').EventEmitter; | |
var es = require('event-stream'); | |
var util = require('util'); | |
function Logger() { | |
Logger.super_.call(this); | |
} | |
util.inherits(Logger, EventEmitter); | |
['info', 'warn', 'error'].forEach(function(level) { | |
Logger.prototype[level] = function(msg) { | |
var line = util.format('[%s] %s', level, msg); | |
this.emit('log', {level: level, line: line}); | |
}; | |
}); | |
Logger.prototype.getStream = function(levels) { | |
var stream = es.through(); | |
var write = stream.write.bind(stream); | |
stream.__end = stream.end; // necessary? | |
stream.write = stream.end = function() {}; | |
this.on('log', function(data) { | |
if (levels.indexOf(data.level) !== -1) { | |
write(data.line + '\n'); | |
} | |
}); | |
stream.bufferedStream = function() { | |
var buffered = es.split(); | |
var self = this; | |
buffered.pipe(es.through(function(line) { | |
write(line + '\n'); | |
})); | |
return buffered; | |
}; | |
return stream; | |
}; | |
var logger = new Logger(); | |
// logger.on('log', function(data) { | |
// console.log(data.line); | |
// }); | |
// logger.info('test'); | |
// logger.warn('123'); | |
// logger.error('oops'); | |
var logStdio = logger.getStream(['info', 'warn']); | |
logStdio.pipe(process.stdout); | |
var logStderr = logger.getStream(['error']); | |
logStderr.pipe(process.stderr); | |
logger.info('test'); | |
logger.warn('123'); | |
logger.error('oops'); | |
var evil = es.through(); | |
evil.pipe(logStdio); // pipe to the main stream? O NOES | |
evil.write('THIS SHOULD BREAK NOTHING'); evil.end(); | |
var s1 = logStdio.bufferedStream(); | |
s1.write('st'); | |
logger.info('test'); | |
s1.write('do'); | |
logger.warn('123'); | |
s1.write('ut'); s1.end(); | |
logger.error('oops'); | |
var s2 = logStdio.bufferedStream(); | |
var t2 = es.through(function(s) { this.queue(s.toUpperCase()); }); | |
t2.pipe(s2); | |
t2.write('st'); | |
logger.info('test'); | |
t2.write('do'); | |
logger.warn('123'); | |
t2.write('ut'); t2.end(); | |
logger.error('oops'); | |
var s3 = logStderr.bufferedStream(); | |
var t2 = es.through(); | |
t2.pipe(s3); | |
t2.write('st'); | |
logger.info('test'); | |
t2.write('de'); | |
logger.warn('123'); | |
t2.write('rr'); t2.end(); | |
logger.error('oops'); |
This file contains 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
$ node muxing-logger.js | |
[info] test | |
[warn] 123 | |
[error] oops | |
[info] test | |
[warn] 123 | |
stdout | |
[error] oops | |
[info] test | |
[warn] 123 | |
STDOUT | |
[error] oops | |
[info] test | |
[warn] 123 | |
stderr | |
[error] oops | |
$ node muxing-logger.js >/dev/null | |
[error] oops | |
[error] oops | |
[error] oops | |
stderr | |
[error] oops | |
$ node muxing-logger.js 2>/dev/null | |
[info] test | |
[warn] 123 | |
[info] test | |
[warn] 123 | |
stdout | |
[info] test | |
[warn] 123 | |
STDOUT | |
[info] test | |
[warn] 123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment