Added support for Gzip log files

This commit is contained in:
Michael Hart 2012-06-22 15:14:56 +10:00
parent efab2b0b3f
commit ad71b88f96
3 changed files with 43 additions and 2 deletions

View file

@ -698,7 +698,12 @@ function processStdin(opts, stylize, callback) {
* @param callback {Function} `function ()` * @param callback {Function} `function ()`
*/ */
function processFile(file, opts, stylize, callback) { function processFile(file, opts, stylize, callback) {
var stream = fs.createReadStream(file, {encoding: 'utf8'}); var stream = fs.createReadStream(file);
if (/\.gz$/.test(file)) {
stream = stream.pipe(require('zlib').createGunzip());
}
// Manually decode streams - lazy load here as per node/lib/fs.js
var decoder = new (require('string_decoder').StringDecoder)('utf8');
streams[file].stream = stream; streams[file].stream = stream;
@ -708,7 +713,11 @@ function processFile(file, opts, stylize, callback) {
}); });
var leftover = ''; // Left-over partial line from last chunk. var leftover = ''; // Left-over partial line from last chunk.
stream.on('data', function (chunk) { stream.on('data', function (data) {
var chunk = decoder.write(data);
if (!chunk.length) {
return;
}
var lines = chunk.split(/\r\n|\n/); var lines = chunk.split(/\r\n|\n/);
var length = lines.length; var length = lines.length;
if (length === 1) { if (length === 1) {

View file

@ -168,3 +168,35 @@ test('multiple logs', function (t) {
t.end(); t.end();
}); });
}); });
test('log1.log.gz', function (t) {
exec(BUNYAN + ' corpus/log1.log.gz', function (err, stdout, stderr) {
t.error(err);
t.equal(stdout, [
'[2012-05-08T16:57:55.586Z] INFO: agent1/73267 on headnode: message\n',
'[2012-05-08T17:02:49.339Z] INFO: agent1/73267 on headnode: message\n',
'[2012-05-08T17:02:49.404Z] INFO: agent1/73267 on headnode: message\n',
'[2012-05-08T17:02:49.404Z] INFO: agent1/73267 on headnode: message\n',
].join(''));
t.end();
});
});
test('mixed text and gzip logs', function (t) {
exec(BUNYAN + ' corpus/log1.log.gz corpus/log2.log',
function (err, stdout, stderr) {
t.error(err);
t.equal(stdout, [
'[2012-05-08T16:57:55.586Z] INFO: agent1/73267 on headnode: message\n',
'[2012-05-08T16:58:55.586Z] INFO: agent2/73267 on headnode: message\n',
'[2012-05-08T17:01:49.339Z] INFO: agent2/73267 on headnode: message\n',
'[2012-05-08T17:02:47.404Z] INFO: agent2/73267 on headnode: message\n',
'[2012-05-08T17:02:49.339Z] INFO: agent1/73267 on headnode: message\n',
'[2012-05-08T17:02:49.404Z] INFO: agent1/73267 on headnode: message\n',
'[2012-05-08T17:02:49.404Z] INFO: agent1/73267 on headnode: message\n',
'[2012-05-08T17:02:57.404Z] INFO: agent2/73267 on headnode: message\n',
'[2012-05-08T17:08:01.105Z] INFO: agent2/76156 on headnode: message\n',
].join(''));
t.end();
});
});

BIN
test/corpus/log1.log.gz Normal file

Binary file not shown.