From 14a356fcbe79fa8c0296a5c4ec63423fcac82af7 Mon Sep 17 00:00:00 2001 From: Cheolgoo Kang Date: Tue, 2 Dec 2014 13:58:05 -0800 Subject: [PATCH] [fix] DailyRotateFile is now writing to the correct file. --- lib/winston/transports/daily-rotate-file.js | 21 ++++--- test/transports/daily-rotate-file-test.js | 61 +++++++++++++++++++++ 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/lib/winston/transports/daily-rotate-file.js b/lib/winston/transports/daily-rotate-file.js index f81a016f8..b32cf4c98 100644 --- a/lib/winston/transports/daily-rotate-file.js +++ b/lib/winston/transports/daily-rotate-file.js @@ -441,6 +441,18 @@ DailyRotateFile.prototype._createStream = function () { var self = this; this.opening = true; + var now = new Date(); + var timePassed = false; + if (self._year < now.getFullYear() || self._month < now.getMonth() || self._date < now.getDate() || self._hour < now.getHours() || self._minute < now.getMinutes()) { + self._year = now.getFullYear(); + self._month = now.getMonth(); + self._date = now.getDate(); + self._hour = now.getHours(); + self._minute = now.getMinutes(); + self._created = 0; + timePassed = true; + } + (function checkFile (target) { var fullname = path.join(self.dirname, target); @@ -501,14 +513,7 @@ DailyRotateFile.prototype._createStream = function () { return checkFile(self._getFile(true)); } - var now = new Date(); - if (self._year < now.getFullYear() || self._month < now.getMonth() || self._date < now.getDate() || self._hour < now.getHours() || self._minute < now.getMinutes()) { - self._year = now.getFullYear(); - self._month = now.getMonth(); - self._date = now.getDate(); - self._hour = now.getHours(); - self._minute = now.getMinutes(); - self._created = 0; + if (timePassed) { return checkFile(self._getFile()); } diff --git a/test/transports/daily-rotate-file-test.js b/test/transports/daily-rotate-file-test.js index 02681c23d..7cfb87a7c 100644 --- a/test/transports/daily-rotate-file-test.js +++ b/test/transports/daily-rotate-file-test.js @@ -24,6 +24,33 @@ var stream = fs.createWriteStream( }), streamTransport = new (winston.transports.DailyRotateFile)({ stream: stream }); +var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhM])\1?/g; + +function pad(val, len) { + val = String(val); + len = len || 2; + while (val.length < len) { val = "0" + val; } + return val; +}; + +function getFormattedDate(pattern, date) { + var flags = { + yy: String(date.getFullYear()).slice(2), + yyyy: date.getFullYear(), + M: date.getMonth() + 1, + MM: pad(date.getMonth() + 1), + d: date.getDate(), + dd: pad(date.getDate()), + H: date.getHours(), + HH: pad(date.getHours()), + m: date.getMinutes(), + mm: pad(date.getMinutes()) + }; + return pattern.replace(token, function ($0) { + return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1); + }); +} + vows.describe('winston/transports/daily-rotate-file').addBatch({ "An instance of the Daily Rotate File Transport": { "when passed a valid filename": { @@ -59,4 +86,38 @@ vows.describe('winston/transports/daily-rotate-file').addBatch({ filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testfile.log'), datePattern: '.2012-12-18' }) +}).addBatch({ + "An instance of the Daily Rotate File Transport": { + "when the file currently pointing was removed and time has been passing": { + topic: function() { + var self = this; + var minutely = new (winston.transports.DailyRotateFile)({ + filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testfile.log'), + datePattern: '.yyyyMMddHHmm' + }); + // log into the current file + minutely.log('hello'); + var oldTimestamp = getFormattedDate('.yyyyMMddHHmm', new Date()); + setTimeout(function() { + // remove the current file + fs.unlinkSync(path.join(__dirname, '..', 'fixtures', 'logs', 'testfile.log' + oldTimestamp)); + setTimeout(function() { + // wait for a minute and log something + minutely.log('hello AGAIN'); + var newTimestamp = getFormattedDate('.yyyyMMddHHmm', new Date()); + setTimeout(function() { + fs.stat(path.join(__dirname, '..', 'fixtures', 'logs', 'testfile.log' + newTimestamp), self.callback); + }, 1000); + }, (60 - new Date().getSeconds() + 1) * 1000); + }, 3 * 1000); + }, + "should be logged into the new file": function(err, stat) { + // see if the file with new timestamp exists + assert.isNull(err); + assert.isNotNull(stat); + assert.isDefined(stat); + assert.isTrue(stat.size > 0); + } + } + } }).export(module);