issue #104: `log.reopenFileStreams()` convenience method to be used with external log rotation.

master
Trent Mick 2013-10-11 13:16:29 -07:00
parent 96e2a9b31c
commit eac13c08cc
5 changed files with 61 additions and 5 deletions

View File

@ -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

View File

@ -720,6 +720,23 @@ used for anything else.</td>
</table>
**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`

View File

@ -6,7 +6,7 @@
// See <https://github.com/trentm/node-bunyan>.
//
var VERSION = '0.21.5';
var VERSION = '0.22.0';
var p = console.log;
var util = require('util');

View File

@ -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 <https://github.com/trentm/node-bunyan/issues/104>.
*/
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.

View File

@ -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 <trentm@gmail.com> (http://trentm.com)",
"main": "./lib/bunyan.js",