Style/changelog/readme/test case for "error" event re-emitting.

PR: #318
This commit is contained in:
Trent Mick 2016-02-10 23:38:23 -08:00
parent e0e06d3af5
commit 6fdc5ff209
7 changed files with 55 additions and 11 deletions

View file

@ -29,3 +29,4 @@ https://github.com/sometimesalready
Charly Koza (https://github.com/Cactusbone) Charly Koza (https://github.com/Cactusbone)
Thomas Heymann (https://github.com/cyberthom) Thomas Heymann (https://github.com/cyberthom)
David M. Lee (https://github.com/leedm777) David M. Lee (https://github.com/leedm777)
Marc Udoff (https://github.com/mlucool)

View file

@ -6,9 +6,13 @@ Known issues:
bug](https://github.com/TooTallNate/node-gyp/issues/65). 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 ## 1.6.0

View file

@ -593,7 +593,7 @@ type "stream" emitting to `process.stdout` at the "info" level.
## stream errors ## 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: do this:
```js ```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 As of bunyan@1.7.0, "error" events are re-emitted for any stream, as long as
produced by `log.error(...)`. 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` ## stream type: `stream`

View file

@ -11,7 +11,7 @@
* vim: expandtab:ts=4:sw=4 * vim: expandtab:ts=4:sw=4
*/ */
var VERSION = '1.6.1'; var VERSION = '1.7.0';
var p = console.log; var p = console.log;
var util = require('util'); var util = require('util');

View file

@ -8,7 +8,7 @@
* vim: expandtab:ts=4:sw=4 * 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. * 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 + '"'); throw new TypeError('unknown stream type "' + s.type + '"');
} }
if(typeof s.stream.on === 'function') { if (typeof (s.stream.on) === 'function') {
s.stream.on('error', function (err) { s.stream.on('error', function onStreamError(err) {
self.emit('error', err, s); self.emit('error', err, s);
}); });
} }
self.streams.push(s); self.streams.push(s);
delete self.haveNonRawStreams; // reset delete self.haveNonRawStreams; // reset
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "bunyan", "name": "bunyan",
"version": "1.6.1", "version": "1.7.0",
"description": "a JSON logging library for node.js services", "description": "a JSON logging library for node.js services",
"author": "Trent Mick <trentm@gmail.com> (http://trentm.com)", "author": "Trent Mick <trentm@gmail.com> (http://trentm.com)",
"main": "./lib/bunyan.js", "main": "./lib/bunyan.js",

View file

@ -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' * Test emission and handling of 'error' event in a logger with a 'path'
* stream. * stream.
*/ */
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var bunyan = require('../lib/bunyan'); var bunyan = require('../lib/bunyan');
// node-tap API // node-tap API
@ -30,3 +33,35 @@ test('error event on log write', function (t) {
}); });
log.info('info log message'); 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');
});