Skip to content

Commit

Permalink
[fix] catch exceptions for file transport unlinkSync
Browse files Browse the repository at this point in the history
Using cluster, I sometimes run more than one winston logger on a
single instance. Occasionally they both detect that they should
remove the log file, and one will unlink before the other. This
adds a try...catch to make sure that the unlinkSync call doesn't
throw.
  • Loading branch information
Calvin French-Owen authored and indexzero committed Oct 6, 2014
1 parent e44270d commit 51b4ea3
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,15 @@ File.prototype._getFile = function (inc) {
// Check for maxFiles option and delete file
if (this.maxFiles && (this._created >= (this.maxFiles - 1))) {
remaining = this._created - (this.maxFiles - 1);
if (remaining === 0) {
fs.unlinkSync(path.join(this.dirname, basename + ext));
}
else {
fs.unlinkSync(path.join(this.dirname, basename + remaining + ext));
try {
if (remaining === 0) {
fs.unlinkSync(path.join(this.dirname, basename + ext));
}
else {
fs.unlinkSync(path.join(this.dirname, basename + remaining + ext));
}
} catch (e) {
// If the file was already removed
}
}

Expand Down

0 comments on commit 51b4ea3

Please sign in to comment.