Fixed the file appender tests

This commit is contained in:
Gareth Jones
2011-12-20 08:49:21 +11:00
parent 78de73a274
commit a999d8fc00
4 changed files with 47 additions and 31 deletions
+12 -7
View File
@@ -20,13 +20,18 @@ function fileAppender (file, layout, logSize, numBackups) {
numBackups = numBackups === 0 ? 1 : numBackups; numBackups = numBackups === 0 ? 1 : numBackups;
function openTheStream(file, fileSize, numFiles) { function openTheStream(file, fileSize, numFiles) {
var stream = new streams.BufferedWriteStream( var stream;
new streams.RollingFileStream( if (fileSize) {
file, stream = new streams.BufferedWriteStream(
fileSize, new streams.RollingFileStream(
numFiles file,
) fileSize,
); numFiles
)
);
} else {
stream = new streams.BufferedWriteStream(fs.createWriteStream(file, { encoding: "utf8", mode: 0644, flags: 'a' }));
}
stream.on("error", function (err) { stream.on("error", function (err) {
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err); console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
}); });
+8 -5
View File
@@ -84,11 +84,12 @@ function RollingFileStream (filename, size, backups, options) {
this.options = options || { encoding: "utf8", mode: 0644, flags: 'a' }; this.options = options || { encoding: "utf8", mode: 0644, flags: 'a' };
this.rolling = false; this.rolling = false;
this.writesWhileRolling = []; this.writesWhileRolling = [];
this.bytesQueued = 0;
throwErrorIfArgumentsAreNotValid(); throwErrorIfArgumentsAreNotValid();
RollingFileStream.super_.call(this, this.filename, this.options); RollingFileStream.super_.call(this, this.filename, this.options);
this.bytesWritten = currentFileSize(this.filename); this.bytesQueued = currentFileSize(this.filename);
function currentFileSize(file) { function currentFileSize(file) {
var fileSize = 0; var fileSize = 0;
@@ -114,9 +115,9 @@ RollingFileStream.prototype.write = function(data, encoding) {
return false; return false;
} else { } else {
var canWrite = RollingFileStream.super_.prototype.write.call(this, data, encoding); var canWrite = RollingFileStream.super_.prototype.write.call(this, data, encoding);
//this.bytesWritten += data.length; console.log("bytesQueued: %d, max: %d", this.bytesQueued, this.size);
console.log("bytesWritten: %d, max: %d", this.bytesWritten, this.size); this.bytesQueued += data.length;
if (this.bytesWritten >= this.size) { if (this.bytesQueued >= this.size) {
this.roll(); this.roll();
} }
return canWrite; return canWrite;
@@ -185,6 +186,7 @@ RollingFileStream.prototype.roll = function () {
that.fd = fd; that.fd = fd;
that.writable = true; that.writable = true;
fs.close(oldLogFileFD, function() { fs.close(oldLogFileFD, function() {
that.bytesQueued = 0;
that.bytesWritten = 0; that.bytesWritten = 0;
cb(); cb();
}); });
@@ -197,9 +199,10 @@ RollingFileStream.prototype.roll = function () {
var toWrite; var toWrite;
while ((toWrite = that.writesWhileRolling.shift())) { while ((toWrite = that.writesWhileRolling.shift())) {
RollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding); RollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding);
that.bytesWritten += toWrite.data.length; that.bytesQueued += toWrite.data.length;
} }
that.rolling = false; that.rolling = false;
that.flush();
cb(); cb();
} }
+2 -2
View File
@@ -44,8 +44,8 @@ vows.describe('log4js logLevelFilter').addBatch({
logger.error('both'); logger.error('both');
logger.warn('both'); logger.warn('both');
logger.debug('main'); logger.debug('main');
//wait for the file system to catch up
return logger; setTimeout(this.callback, 100);
}, },
'tmp-tests.log': { 'tmp-tests.log': {
topic: function() { topic: function() {
+25 -17
View File
@@ -90,28 +90,36 @@ vows.describe('RollingFileStream').addBatch({
remove(__dirname + "/test-rolling-file-stream-write-more"); remove(__dirname + "/test-rolling-file-stream-write-more");
remove(__dirname + "/test-rolling-file-stream-write-more.1"); remove(__dirname + "/test-rolling-file-stream-write-more.1");
var that = this, stream = new RollingFileStream(__dirname + "/test-rolling-file-stream-write-more", 45); var that = this, stream = new RollingFileStream(__dirname + "/test-rolling-file-stream-write-more", 45);
stream.on("open", function() { that.callback(null, stream); }); stream.on("open", function() {
},
'(when open)': {
topic: function(stream) {
var that = this;
for (var i=0; i < 7; i++) { for (var i=0; i < 7; i++) {
stream.write(i +".cheese\n", "utf8"); stream.write(i +".cheese\n", "utf8");
} }
stream.end(function (err) { //wait for the file system to catch up with us
fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", that.callback); setTimeout(that.callback, 100);
}); });
},
'the number of files': {
topic: function() {
fs.readdir(__dirname, this.callback);
}, },
'should write to the file': function(contents) { 'should be two': function(files) {
assert.equal(contents, "5.cheese\n6.cheese\n"); assert.equal(files.filter(function(file) { return file.indexOf('test-rolling-file-stream-write-more') > -1; }).length, 2);
}
},
'the first file': {
topic: function() {
fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback);
}, },
'the number of files': { 'should contain the last two log messages': function(contents) {
topic: function() { assert.equal(contents, '5.cheese\n6.cheese\n');
fs.readdir(__dirname, this.callback); }
}, },
'should be two': function(files) { 'the second file': {
assert.equal(files.filter(function(file) { return file.indexOf('test-rolling-file-stream-write-more') > -1; }).length, 2); topic: function() {
} fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback);
},
'should contain the first five log messages': function(contents) {
assert.equal(contents, '0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n');
} }
} }
} }