diff --git a/CHANGES.md b/CHANGES.md index 91fc09f..dab627d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,9 +6,10 @@ Known issues: bug](https://github.com/TooTallNate/node-gyp/issues/65). -## bunyan 0.21.5 (not yet released) +## bunyan 0.22.0 (not yet released) -(nothing yet) +- [issue #104] `log.reopenFileStreams()` convenience method to be used with + external log rotation. ## bunyan 0.21.4 diff --git a/README.md b/README.md index 5360b23..f79c126 100644 --- a/README.md +++ b/README.md @@ -720,6 +720,23 @@ used for anything else. +**Note on log rotation**: Often you may be using external log rotation utilities +like `logrotate` on Linux or `logadm` on SmartOS/Illumos. In those cases, unless +your are ensuring "copy and truncate" sematics (via `copytruncate` with +logrotate or `-c` with logadm) then the fd for your 'file' stream will change. +You can tell bunyan to reopen the file stream with code like this in your +app: + + var log = bunyan.createLogger(...); + ... + process.on('SIGUSR2', function () { + log.reopenFileStreams(); + }); + +where you'd configure your log rotation to send SIGUSR2 (or some other signal) +to your process. Any other mechanism to signal your app to run +`log.reopenFileStreams()` would work as well. + ## stream type: `raw` diff --git a/bin/bunyan b/bin/bunyan index e220fd3..ecfd66d 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -6,7 +6,7 @@ // See . // -var VERSION = '0.21.5'; +var VERSION = '0.22.0'; var p = console.log; var util = require('util'); diff --git a/lib/bunyan.js b/lib/bunyan.js index 6eaf539..3b408f7 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -4,7 +4,7 @@ * The bunyan logging library for node.js. */ -var VERSION = '0.21.5'; +var VERSION = '0.22.0'; // Bunyan log format version. This becomes the 'v' field on all log records. // `0` is until I release a version '1.0.0' of node-bunyan. Thereafter, @@ -508,6 +508,44 @@ Logger.prototype.child = function (options, simple) { } +/** + * A convenience method to reopen 'file' streams on a logger. This can be + * useful with external log rotation utilities that move and re-open log files + * (e.g. logrotate on Linux, logadm on SmartOS/Illumos). Those utilities + * typically have rotation options to copy-and-truncate the log file, but + * you may not want to use that. An alternative is to do this in your + * application: + * + * var log = bunyan.createLogger(...); + * ... + * process.on('SIGUSR2', function () { + * log.reopenFileStreams(); + * }); + * ... + * + * See . + */ +Logger.prototype.reopenFileStreams = function () { + var self = this; + self.streams.forEach(function (s) { + if (s.type === 'file') { + if (s.stream) { + // Not sure if typically would want this, or more immediate + // `s.stream.destroy()`. + s.stream.end(); + s.stream.destroySoon(); + delete s.stream; + } + s.stream = fs.createWriteStream(s.path, + {flags: 'a', encoding: 'utf8'}); + s.stream.on('error', function (err) { + self.emit('error', err, s); + }); + } + }); +}; + + /* BEGIN JSSTYLED */ /** * Close this logger. diff --git a/package.json b/package.json index 164bfe2..ec2b705 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bunyan", - "version": "0.21.5", + "version": "0.22.0", "description": "a JSON Logger library for node.js services", "author": "Trent Mick (http://trentm.com)", "main": "./lib/bunyan.js",