From 51b4ea309a33b4ba827109f46b452e0eaad6c472 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 61d1605f1..61df73b0b 100644 --- a/lib/winston/transports/file.js +++ b/lib/winston/transports/file.js @@ -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 } }