diff --git a/AUTHORS b/AUTHORS index 3977bd9..c5947fa 100644 --- a/AUTHORS +++ b/AUTHORS @@ -29,3 +29,4 @@ https://github.com/sometimesalready Charly Koza (https://github.com/Cactusbone) Thomas Heymann (https://github.com/cyberthom) David M. Lee (https://github.com/leedm777) +Marc Udoff (https://github.com/mlucool) diff --git a/CHANGES.md b/CHANGES.md index 6768836..f82ce3f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,9 +6,13 @@ Known issues: bug](https://github.com/TooTallNate/node-gyp/issues/65). -## 1.6.1 (not yet released) +## 1.7.0 (not yet released) -(nothing yet) +- [pull #318] Re-emit Bunyan stream 'error' events on the Logger instance from + *any stream with a `.on()`* -- which is any that inherits from EventEmitter. + Before this change, 'error' events were only re-emitted on [`file` + streams](https://github.com/trentm/node-bunyan#stream-type-file). + (By Marc Udoff.) ## 1.6.0 diff --git a/README.md b/README.md index d15ff1e..a4ce069 100644 --- a/README.md +++ b/README.md @@ -593,7 +593,7 @@ type "stream" emitting to `process.stdout` at the "info" level. ## stream errors -Bunyan re-emits error events from the created `WriteStream`. So you can +Bunyan re-emits "error" events from the created `WriteStream`. So you can do this: ```js @@ -603,8 +603,11 @@ log.on('error', function (err, stream) { }); ``` -Note: This is **not** that same as a log record at the "error" level as -produced by `log.error(...)`. +As of bunyan@1.7.0, "error" events are re-emitted for any stream, as long as +it has a `.on()` -- e.g. if it inherits from EventEmitter. + +Note: This error eventi is **not** related to log records at the "error" level +as produced by `log.error(...)`. ## stream type: `stream` diff --git a/bin/bunyan b/bin/bunyan index a41aacf..891d823 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -11,7 +11,7 @@ * vim: expandtab:ts=4:sw=4 */ -var VERSION = '1.6.1'; +var VERSION = '1.7.0'; var p = console.log; var util = require('util'); diff --git a/lib/bunyan.js b/lib/bunyan.js index 6adacde..0741759 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -8,7 +8,7 @@ * vim: expandtab:ts=4:sw=4 */ -var VERSION = '1.6.1'; +var VERSION = '1.7.0'; /* * Bunyan log format version. This becomes the 'v' field on all log records. @@ -576,11 +576,12 @@ Logger.prototype.addStream = function addStream(s, defaultLevel) { throw new TypeError('unknown stream type "' + s.type + '"'); } - if(typeof s.stream.on === 'function') { - s.stream.on('error', function (err) { + if (typeof (s.stream.on) === 'function') { + s.stream.on('error', function onStreamError(err) { self.emit('error', err, s); }); } + self.streams.push(s); delete self.haveNonRawStreams; // reset } diff --git a/package.json b/package.json index 28ff9b2..739a037 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bunyan", - "version": "1.6.1", + "version": "1.7.0", "description": "a JSON logging library for node.js services", "author": "Trent Mick (http://trentm.com)", "main": "./lib/bunyan.js", diff --git a/test/error-event.test.js b/test/error-event.test.js index 50616b3..af9beca 100644 --- a/test/error-event.test.js +++ b/test/error-event.test.js @@ -1,10 +1,13 @@ /* - * Copyright (c) 2012 Trent Mick. All rights reserved. + * Copyright 2016 Trent Mick. All rights reserved. * * Test emission and handling of 'error' event in a logger with a 'path' * stream. */ +var EventEmitter = require('events').EventEmitter; +var util = require('util'); + var bunyan = require('../lib/bunyan'); // node-tap API @@ -30,3 +33,35 @@ test('error event on log write', function (t) { }); log.info('info log message'); }); + + +function MyErroringStream() { + +} +util.inherits(MyErroringStream, EventEmitter); + +MyErroringStream.prototype.write = function (rec) { + this.emit('error', new Error('boom')); +} + +test('error event on log write (raw stream)', function (t) { + LOG_PATH = '/this/path/is/bogus.log' + var log = bunyan.createLogger({ + name: 'error-event-raw', + streams: [ + { + stream: new MyErroringStream(), + type: 'raw' + } + ] + }); + log.on('error', function (err, stream) { + t.ok(err, 'got err in error event: ' + err); + t.equal(err.message, 'boom'); + t.ok(stream, 'got a stream argument'); + t.ok(stream.stream instanceof MyErroringStream); + t.equal(stream.type, 'raw'); + t.end(); + }); + log.info('info log message'); +});