Log compression.
This commit is contained in:
@@ -27,7 +27,7 @@ process.on('exit', function() {
|
|||||||
* @param numBackups - the number of log files to keep after logSize
|
* @param numBackups - the number of log files to keep after logSize
|
||||||
* has been reached (default 5)
|
* has been reached (default 5)
|
||||||
*/
|
*/
|
||||||
function fileAppender (file, layout, logSize, numBackups, level) {
|
function fileAppender (file, layout, logSize, numBackups, level, compress) {
|
||||||
var bytesWritten = 0;
|
var bytesWritten = 0;
|
||||||
file = path.normalize(file);
|
file = path.normalize(file);
|
||||||
layout = layout || layouts.basicLayout;
|
layout = layout || layouts.basicLayout;
|
||||||
@@ -41,7 +41,8 @@ function fileAppender (file, layout, logSize, numBackups, level) {
|
|||||||
stream = new streams.RollingFileStream(
|
stream = new streams.RollingFileStream(
|
||||||
file,
|
file,
|
||||||
fileSize,
|
fileSize,
|
||||||
numFiles
|
numFiles,
|
||||||
|
{ "compress": compress }
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
stream = fs.createWriteStream(
|
stream = fs.createWriteStream(
|
||||||
@@ -90,7 +91,7 @@ function configure(config, options) {
|
|||||||
config.filename = path.join(options.cwd, config.filename);
|
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) {
|
function shutdown(cb) {
|
||||||
|
|||||||
@@ -16,7 +16,11 @@ module.exports = BaseRollingFileStream;
|
|||||||
function BaseRollingFileStream(filename, options) {
|
function BaseRollingFileStream(filename, options) {
|
||||||
debug("In BaseRollingFileStream");
|
debug("In BaseRollingFileStream");
|
||||||
this.filename = filename;
|
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;
|
this.currentSize = 0;
|
||||||
|
|
||||||
function currentFileSize(file) {
|
function currentFileSize(file) {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ var BaseRollingFileStream = require('./BaseRollingFileStream')
|
|||||||
, debug = require('../debug')('RollingFileStream')
|
, debug = require('../debug')('RollingFileStream')
|
||||||
, util = require('util')
|
, util = require('util')
|
||||||
, path = require('path')
|
, path = require('path')
|
||||||
|
, child_process = require('child_process')
|
||||||
|
, zlib = require("zlib")
|
||||||
, fs = require('fs')
|
, fs = require('fs')
|
||||||
, async = require('async');
|
, async = require('async');
|
||||||
|
|
||||||
@@ -25,7 +27,7 @@ function RollingFileStream (filename, size, backups, options) {
|
|||||||
util.inherits(RollingFileStream, BaseRollingFileStream);
|
util.inherits(RollingFileStream, BaseRollingFileStream);
|
||||||
|
|
||||||
RollingFileStream.prototype.shouldRoll = function() {
|
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;
|
return this.currentSize >= this.size;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -38,6 +40,7 @@ RollingFileStream.prototype.roll = function(filename, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function index(filename_) {
|
function index(filename_) {
|
||||||
|
debug('Calculating index of '+filename_);
|
||||||
return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0;
|
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) {
|
function increaseFileIndex (fileToRename, cb) {
|
||||||
var idx = index(fileToRename);
|
var idx = index(fileToRename);
|
||||||
debug('Index of ' + fileToRename + ' is ' + idx);
|
debug('Index of ' + fileToRename + ' is ' + idx);
|
||||||
if (idx < that.backups) {
|
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
|
//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
|
//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
|
//ignore err: if we could not delete, it's most likely that it doesn't exist
|
||||||
debug('Renaming ' + fileToRename + ' -> ' + filename + '.' + (idx+1));
|
debug('Renaming ' + fileToRename + ' -> ' + destination);
|
||||||
fs.rename(path.join(path.dirname(filename), fileToRename), filename + '.' + (idx + 1), cb);
|
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 {
|
} else {
|
||||||
cb();
|
cb();
|
||||||
|
|||||||
Reference in New Issue
Block a user