From 2c2459df2f871d9d3dc0696b024e86107328f586 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Sun, 1 Jun 2014 23:34:50 -0700 Subject: [PATCH] #117 add warning about using rotating-file and cluster together --- README.md | 13 +++++++++++-- lib/bunyan.js | 12 +++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ed261f3..31dca52 100644 --- a/README.md +++ b/README.md @@ -643,13 +643,22 @@ used for anything else. ## stream type: `rotating-file` -**WARNING:** Users of Bunyan's `rotating-file` should (a) be using at least -bunyan 0.23.1 (with the fix for [this +**WARNING on node 0.8 usage:** Users of Bunyan's `rotating-file` should (a) be +using at least bunyan 0.23.1 (with the fix for [this issue](https://github.com/trentm/node-bunyan/pull/97)), and (b) should use at least node 0.10 (node 0.8 does not support the `unref()` method on `setTimeout(...)` needed for the mentioned fix). The symptom is that process termination will hang for up to a full rotation period. +**WARNING on [cluster](http://nodejs.org/docs/latest/api/all.html#all_cluster) +usage:** Using Bunyan's `rotating-file` stream with node.js's "cluster" module +can result in unexpected file rotation. You must not have multiple processes +in the cluster logging to the same file path. In other words, you must have +a separate log file path for the master and each worker in the cluster. +Alternatively, consider using a system file rotation facility such as +`logrotate` on Linux or `logadm` on SmartOS/Illumos. See +[this comment on issue #117](https://github.com/trentm/node-bunyan/issues/117#issuecomment-44804938) +for details. A `type === 'rotating-file'` is a file stream that handles file automatic rotation. diff --git a/lib/bunyan.js b/lib/bunyan.js index 1ea6ea4..f4b84ce 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -1107,7 +1107,10 @@ RotatingFileStream.prototype.rotate = function rotate() { var self = this; var _DEBUG = false; - if (_DEBUG) console.log('-- [%s] rotating %s', new Date(), self.path); + if (_DEBUG) { + console.log('-- [%s, pid=%s] rotating %s', + new Date(), process.pid, self.path); + } if (self.rotating) { throw new TypeError('cannot start a rotation when already rotating'); } @@ -1142,7 +1145,10 @@ RotatingFileStream.prototype.rotate = function rotate() { if (!exists) { moves(); } else { - if (_DEBUG) console.log('mv %s %s', before, after); + if (_DEBUG) { + console.log('[pid %s] mv %s %s', + process.pid, before, after); + } mv(before, after, function (mvErr) { if (mvErr) { self.emit('error', mvErr); @@ -1156,7 +1162,7 @@ RotatingFileStream.prototype.rotate = function rotate() { } function finish() { - if (_DEBUG) console.log('open %s', self.path); + if (_DEBUG) console.log('[pid %s] open %s', process.pid, self.path); self.stream = fs.createWriteStream(self.path, {flags: 'a', encoding: 'utf8'}); var q = self.rotQueue, len = q.length;