Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] DailyRotateFile is now writing to the correct file. #505

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/winston/transports/daily-rotate-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
}

Expand Down
61 changes: 61 additions & 0 deletions test/transports/daily-rotate-file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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);