Skip to content

Commit

Permalink
Allowing the startNewFile flag to work on numbered rotating files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Tupper committed Feb 28, 2016
1 parent ae20de8 commit 64aec51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lib/datestampedfileops.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,12 @@ function DateStampedFileOps(logpath, totalFiles, totalSize, gzip) {
function newStreamFilepath(triggerinfo, next) {
filenameTimestamp = new Date(triggerinfo.date || Date.now());

var startNewFile = triggerinfo.hasOwnProperty('startNewFile') &&
!triggerinfo.startNewFile;
var startNewFile = !triggerinfo.hasOwnProperty('startNewFile') ||
triggerinfo.startNewFile;

if (startNewFile) {
createNewFile(next);
} else {
getSortedLogFiles(function (err, logfiles) {
if (err) {
return next(err);
Expand All @@ -231,8 +233,6 @@ function DateStampedFileOps(logpath, totalFiles, totalSize, gzip) {
next(null, logfiles[0].path);
}
});
} else {
createNewFile(next);
}

}
Expand Down
15 changes: 14 additions & 1 deletion lib/numberedfileops.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,20 @@ function NumberedFileOps(logpath, totalFiles, totalSize, gzip) {
}

function newStreamFilepath(triggerinfo, next) {
next(null, getNumberedFile(0, false));
var startNewFile = !triggerinfo.hasOwnProperty('startNewFile') ||
triggerinfo.startNewFile;

if (startNewFile) {
moveIntermediateFiles(function (err) {
if (err) {
return next(err);
}

next(null, getNumberedFile(0, false));
});
} else {
next(null, getNumberedFile(0, false));
}
}

return {
Expand Down
27 changes: 15 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ function basicthreshold(template) {
}
}

function reusefiles(template, reuse) {
function forcenewfile(template, forcenew) {
return function (next) {
var name = 'testlogs/' + 'reusetimestampedfiles-' + template + (reuse ? '-reused' : '-newfile');
var name = 'testlogs/' + 'forcenewfile-' + template + (forcenew ? '-newfile' : '-reused');

async.series([
function (next) { rmdir(name, ignoreMissing(next)); },
function (next) { fx.mkdir(name, next); },
function (next) { runTest (name, {
stream: { path: name + '/' + template + '.log', startNewFile: !reuse, threshold: 800 },
stream: { path: name + '/' + template + '.log', startNewFile: forcenew, threshold: 800 },
batch: { iterations: 10 }
}, next); },
function (next) {
Expand All @@ -195,12 +195,13 @@ function reusefiles(template, reuse) {
setTimeout(next, 2000);
},
function (next) { runTest (name, {
stream: { path: name + '/' + template + '.log', startNewFile: !reuse, threshold: 800 },
stream: { path: name + '/' + template + '.log', startNewFile: forcenew, threshold: 800 },
batch: { iterations: 1 }
}, next); },
function (next) {
var files = fs.readdirSync(name);
assert.equal(reuse ? 2 : 3, files.length, 'Wrong number of files for ' + name.replace('%d', '%%d') + JSON.stringify(files));
var expectedfiles = forcenew ? 3 : 2
assert.equal(expectedfiles, files.length, 'Wrong number of files for ' + name.replace('%d', '%%d') + ' expected: ' + expectedfiles + ' got: ' + JSON.stringify(files));
console.log(name.replace('%d', '%%d'), 'passed');
next();
},
Expand Down Expand Up @@ -532,13 +533,15 @@ fx.mkdir('testlogs', function () {
basicthreshold('test-%Y-%m-%d'),
basicthreshold('test-%Y-%m-%d-%H-%M-%S'),
basicthreshold('test-%N-%Y-%m-%d'),
reusefiles('test', true),
reusefiles('test-%N', true),
reusefiles('test-%Y-%m-%d', true),
reusefiles('test-%Y-%m-%d-%H-%M-%S', true),
reusefiles('test-%Y-%m-%d', false),
reusefiles('test-%Y-%m-%d-%H-%M-%S', false),
reusefiles('test-%N-%Y-%m-%d', false),
forcenewfile('test', true),
forcenewfile('test-%N', true),
forcenewfile('test', false),
forcenewfile('test-%N', false),
forcenewfile('test-%Y-%m-%d', true),
forcenewfile('test-%Y-%m-%d-%H-%M-%S', true),
forcenewfile('test-%Y-%m-%d', false),
forcenewfile('test-%Y-%m-%d-%H-%M-%S', false),
forcenewfile('test-%N-%Y-%m-%d', false),
timerotation('test'),
timerotation('test-%N'),
timerotation('test-%Y-%m-%d'),
Expand Down

0 comments on commit 64aec51

Please sign in to comment.