From 5c217212bd63d53639d980cf922c467e9cf7dd0f Mon Sep 17 00:00:00 2001 From: Calvin French-Owen Date: Thu, 6 Jun 2013 21:38:00 -0700 Subject: [PATCH] [fix] catch exceptions for file transport unlinkSync 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. --- lib/winston/transports/file.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/winston/transports/file.js b/lib/winston/transports/file.js index 9917311e4..04bda7fe2 100644 --- a/lib/winston/transports/file.js +++ b/lib/winston/transports/file.js @@ -553,11 +553,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 } }