Skip to content

Instantly share code, notes, and snippets.

@Unitecho
Created May 10, 2013 08:22
Show Gist options
  • Save Unitecho/5553150 to your computer and use it in GitHub Desktop.
Save Unitecho/5553150 to your computer and use it in GitHub Desktop.
Common Log streamers
var fs = require('fs');
var path = '/home/tknew/.forever/out-probe2.log';
var colors = [
'\x1B[34m',
'\x1B[36m',
'\x1B[32m',
'\x1B[35m',
'\x1B[31m',
'\x1B[30m',
'\x1B[90m',
'\x1B[33m'
];
var gl_idx = 0;
var db = [];
function stream_log(title, path) {
var currSize = fs.statSync(path).size;
var odb = db[title] = {color : colors[gl_idx++]};
fs.watch(path, function(ev, filename) {
if (ev == 'rename')
return console.error('Renaming file ?');
fs.stat(path, function(err, stat) {
var prevSize = stat.size;
if (currSize > prevSize) return true;
var rstream = fs.createReadStream(path, {
encoding : 'utf8',
start : currSize,
end : prevSize
});
rstream.on('data', function(data) {
console.log(odb.color + '[%s]\x1B[39m %s', title, data.replace('\n', ''));
});
currSize = stat.size;
return true;
});
return true;
});
}
stream_log('probe1', '/home/tknew/.forever/out-probe2.log');
stream_log('probe2', '/home/tknew/.forever/out-probe1.log');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment