From 036293db4193556d6999cba022a5a79b83697342 Mon Sep 17 00:00:00 2001 From: Luis Malheiro Date: Wed, 27 Aug 2014 16:08:17 +0200 Subject: [PATCH] Log compression. --- lib/appenders/file.js | 7 +++--- lib/streams/BaseRollingFileStream.js | 6 ++++- lib/streams/RollingFileStream.js | 37 +++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/appenders/file.js b/lib/appenders/file.js index d7ce702..b79d4f6 100644 --- a/lib/appenders/file.js +++ b/lib/appenders/file.js @@ -27,7 +27,7 @@ process.on('exit', function() { * @param numBackups - the number of log files to keep after logSize * has been reached (default 5) */ -function fileAppender (file, layout, logSize, numBackups, level) { +function fileAppender (file, layout, logSize, numBackups, level, compress) { var bytesWritten = 0; file = path.normalize(file); layout = layout || layouts.basicLayout; @@ -41,7 +41,8 @@ function fileAppender (file, layout, logSize, numBackups, level) { stream = new streams.RollingFileStream( file, fileSize, - numFiles + numFiles, + { "compress": compress } ); } else { stream = fs.createWriteStream( @@ -90,7 +91,7 @@ function configure(config, options) { config.filename = path.join(options.cwd, config.filename); } - return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.level); + return fileAppender(config.filename, layout, config.maxLogSize, config.backups, config.level, config.compress); } function shutdown(cb) { diff --git a/lib/streams/BaseRollingFileStream.js b/lib/streams/BaseRollingFileStream.js index 572794c..9c441ad 100644 --- a/lib/streams/BaseRollingFileStream.js +++ b/lib/streams/BaseRollingFileStream.js @@ -16,7 +16,11 @@ module.exports = BaseRollingFileStream; function BaseRollingFileStream(filename, options) { debug("In BaseRollingFileStream"); this.filename = filename; - this.options = options || { encoding: 'utf8', mode: parseInt('0644', 8), flags: 'a' }; + this.options = options || {}; + this.options.encoding = this.options.encoding || 'utf8'; + this.options.mode = this.options.mode || parseInt('0644', 8); + this.options.flags = this.options.flags || 'a'; + this.currentSize = 0; function currentFileSize(file) { diff --git a/lib/streams/RollingFileStream.js b/lib/streams/RollingFileStream.js index 64a0725..64058f3 100644 --- a/lib/streams/RollingFileStream.js +++ b/lib/streams/RollingFileStream.js @@ -3,6 +3,8 @@ var BaseRollingFileStream = require('./BaseRollingFileStream') , debug = require('../debug')('RollingFileStream') , util = require('util') , path = require('path') +, child_process = require('child_process') +, zlib = require("zlib") , fs = require('fs') , async = require('async'); @@ -25,7 +27,7 @@ function RollingFileStream (filename, size, backups, options) { util.inherits(RollingFileStream, BaseRollingFileStream); RollingFileStream.prototype.shouldRoll = function() { - debug("should roll with current size %d, and max size %d", this.currentSize, this.size); + debug("should roll with current size " + this.currentSize + " and max size " + this.size); return this.currentSize >= this.size; }; @@ -38,6 +40,7 @@ RollingFileStream.prototype.roll = function(filename, callback) { } function index(filename_) { + debug('Calculating index of '+filename_); return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0; } @@ -51,16 +54,42 @@ RollingFileStream.prototype.roll = function(filename, callback) { } } + function compress (filename, cb) { + + var gzip = zlib.createGzip(); + var inp = fs.createReadStream(filename); + var out = fs.createWriteStream(filename+".gz"); + inp.pipe(gzip).pipe(out); + fs.unlink(filename, cb); + + } + function increaseFileIndex (fileToRename, cb) { var idx = index(fileToRename); debug('Index of ' + fileToRename + ' is ' + idx); if (idx < that.backups) { + + var ext = path.extname(fileToRename); + var destination = filename + '.' + (idx+1); + if (that.options.compress && /^gz$/.test(ext.substring(1))) { + destination+=ext; + } //on windows, you can get a EEXIST error if you rename a file to an existing file //so, we'll try to delete the file we're renaming to first - fs.unlink(filename + '.' + (idx+1), function (err) { + fs.unlink(destination, function (err) { //ignore err: if we could not delete, it's most likely that it doesn't exist - debug('Renaming ' + fileToRename + ' -> ' + filename + '.' + (idx+1)); - fs.rename(path.join(path.dirname(filename), fileToRename), filename + '.' + (idx + 1), cb); + debug('Renaming ' + fileToRename + ' -> ' + destination); + fs.rename(path.join(path.dirname(filename), fileToRename), destination, function(err) { + if (err) { + cb(err); + } else { + if (that.options.compress && ext!=".gz") { + compress(destination, cb); + } else { + cb(); + } + } + }); }); } else { cb();