Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added an option to compress rolled dateFile logs #171

Open
wants to merge 1 commit into from

1 participant

@sudowork

Usage:

log4js.configure({
  "appenders": [ {
    "type": "dateFile",
    "filename": "blah.log",
    "pattern": "-yyyy-MM-dd",
    "alwaysIncludePattern": false,
    "compress": true // new option
  } ]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 26, 2013
  1. @sudowork

    Added an option to compress rolled dateFile logs

    sudowork authored sudowork committed
This page is out of date. Refresh to see the latest.
View
15 lib/appenders/dateFile.js
@@ -18,15 +18,19 @@ process.on('exit', function() {
* @filename base filename.
* @pattern the format that will be added to the end of filename when rolling,
* also used to check when to roll files - defaults to '.yyyy-MM-dd'
+ * @compress boolean that dictates if rolled files should be compressed
* @layout layout function for log messages - defaults to basicLayout
*/
-function appender(filename, pattern, alwaysIncludePattern, layout) {
+function appender(filename, pattern, alwaysIncludePattern, compress, layout) {
layout = layout || layouts.basicLayout;
var logFile = new streams.DateRollingFileStream(
filename,
pattern,
- { alwaysIncludePattern: alwaysIncludePattern }
+ {
+ alwaysIncludePattern: alwaysIncludePattern,
+ compress: compress
+ }
);
openFiles.push(logFile);
@@ -46,12 +50,17 @@ function configure(config, options) {
if (!config.alwaysIncludePattern) {
config.alwaysIncludePattern = false;
}
+
+ if (!config.compress) {
+ config.compress = false;
+ }
if (options && options.cwd && !config.absolute) {
config.filename = path.join(options.cwd, config.filename);
}
- return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout);
+ return appender(config.filename, config.pattern, config.alwaysIncludePattern,
+ config.compress, layout);
}
exports.appender = appender;
View
30 lib/streams/DateRollingFileStream.js
@@ -4,7 +4,8 @@ var BaseRollingFileStream = require('./BaseRollingFileStream')
, format = require('../date_format')
, async = require('async')
, fs = require('fs')
-, util = require('util');
+, util = require('util')
+, zlib = require('zlib');
module.exports = DateRollingFileStream;
@@ -20,13 +21,18 @@ function DateRollingFileStream(filename, pattern, options, now) {
this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
this.baseFilename = filename;
this.alwaysIncludePattern = false;
-
+ this.shouldCompress = false;
+
if (options) {
if (options.alwaysIncludePattern) {
this.alwaysIncludePattern = true;
filename = this.baseFilename + this.lastTimeWeWroteSomething;
}
delete options.alwaysIncludePattern;
+ if (options.compress) {
+ this.shouldCompress = true;
+ }
+ delete options.compress;
if (Object.keys(options).length === 0) {
options = null;
}
@@ -59,6 +65,7 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
this.filename = this.baseFilename + this.lastTimeWeWroteSomething;
async.series([
this.closeTheStream.bind(this),
+ compressIfRequested.bind(this),
this.openTheStream.bind(this)
], callback);
} else {
@@ -67,6 +74,7 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
this.closeTheStream.bind(this),
deleteAnyExistingFile,
renameTheCurrentFile,
+ compressIfRequested.bind(this),
this.openTheStream.bind(this)
], callback);
}
@@ -85,4 +93,22 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
fs.rename(filename, newFilename, cb);
}
+ function compressIfRequested(cb) {
+ var fileToCompress = (newFilename) ? newFilename : filename
+ , compressedFile = fileToCompress + '.gz'
+ , inp
+ , out;
+ if (this.shouldCompress) {
+ debug("Compressing " + newFilename);
+ inp = fs.createReadStream(fileToCompress);
+ out = fs.createWriteStream(compressedFile);
+ inp.pipe(zlib.createGzip()).pipe(out);
+ out.on('finish', function() {
+ fs.unlink(fileToCompress, cb);
+ });
+ } else {
+ cb();
+ }
+ }
+
};
View
52 test/streams/DateRollingFileStream-test.js
@@ -3,6 +3,7 @@ var vows = require('vows')
, assert = require('assert')
, fs = require('fs')
, semver = require('semver')
+, zlib = require('zlib')
, streams
, DateRollingFileStream
, testTime = new Date(2012, 8, 12, 10, 37, 11);
@@ -222,6 +223,57 @@ vows.describe('DateRollingFileStream').addBatch({
}
}
}
+ },
+
+ 'with compression': {
+ topic: function() {
+ var that = this
+ , testTime = new Date(2012, 8, 12, 0, 10, 12)
+ , streamName = __dirname + '/test-date-rolling-file-stream-compression'
+ , stream = new DateRollingFileStream(
+ streamName,
+ '.yyyy-MM-dd',
+ {alwaysIncludePattern: true, compress: true},
+ now
+ );
+ stream.write("First message\n", 'utf8', function() {
+ that.callback(null, stream);
+ });
+ },
+
+ 'when the day changes': {
+ topic: function(stream) {
+ testTime = new Date(2012, 8, 13, 0, 10, 12);
+ stream.write("Second message\n", 'utf8', this.callback);
+ },
+ teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-compression.2012-09-13'),
+
+ 'the current log should be created': {
+ topic: function() {
+ fs.readFile(__dirname + '/test-date-rolling-file-stream-compression.2012-09-13',
+ this.callback);
+ },
+
+ 'the contents of the current log should be uncompressed': function(contents) {
+ assert.equal(contents.toString(), "Second message\n");
+ }
+ },
+
+ 'the previous log should be rolled and gzipped': {
+ topic: function() {
+ fs.readFile(__dirname + '/test-date-rolling-file-stream-compression.2012-09-12.gz',
+ this.callback);
+ },
+ teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-compression.2012-09-12.gz'),
+
+ 'the contents should be compressed': function(contents) {
+ zlib.gunzip(contents, function(uncompressed) {
+ assert.equal(uncompressed.toString(), "First message\n");
+ });
+ }
+ }
+
+ }
}
}).exportTo(module);
Something went wrong with that request. Please try again.