2012-06-05 06:13:50 +00:00
|
|
|
/*
|
2016-02-21 02:04:47 +00:00
|
|
|
* Copyright 2016 Trent Mick
|
2012-06-05 06:13:50 +00:00
|
|
|
*
|
|
|
|
* Test emission and handling of 'error' event in a logger with a 'path'
|
|
|
|
* stream.
|
|
|
|
*/
|
|
|
|
|
2016-02-11 07:38:23 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var util = require('util');
|
|
|
|
|
2013-01-07 19:18:12 +00:00
|
|
|
var bunyan = require('../lib/bunyan');
|
|
|
|
|
|
|
|
// node-tap API
|
|
|
|
if (require.cache[__dirname + '/tap4nodeunit.js'])
|
2013-03-29 00:42:32 +00:00
|
|
|
delete require.cache[__dirname + '/tap4nodeunit.js'];
|
2013-01-07 19:18:12 +00:00
|
|
|
var tap4nodeunit = require('./tap4nodeunit.js');
|
|
|
|
var after = tap4nodeunit.after;
|
|
|
|
var before = tap4nodeunit.before;
|
|
|
|
var test = tap4nodeunit.test;
|
|
|
|
|
2012-06-05 06:13:50 +00:00
|
|
|
|
2016-02-21 02:04:47 +00:00
|
|
|
var BOGUS_PATH = '/this/path/is/bogus.log';
|
|
|
|
|
|
|
|
test('error event on file stream (reemitErrorEvents=undefined)', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var log = bunyan.createLogger(
|
2016-02-21 02:04:47 +00:00
|
|
|
{name: 'error-event-1', streams: [ {path: BOGUS_PATH} ]});
|
2013-03-29 00:42:32 +00:00
|
|
|
log.on('error', function (err, stream) {
|
|
|
|
t.ok(err, 'got err in error event: ' + err);
|
|
|
|
t.equal(err.code, 'ENOENT', 'error code is ENOENT');
|
|
|
|
t.ok(stream, 'got a stream argument');
|
2016-02-21 02:04:47 +00:00
|
|
|
t.equal(stream.path, BOGUS_PATH);
|
2013-03-29 00:42:32 +00:00
|
|
|
t.equal(stream.type, 'file');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.info('info log message');
|
2012-06-05 06:13:50 +00:00
|
|
|
});
|
2016-02-11 07:38:23 +00:00
|
|
|
|
2016-02-21 02:04:47 +00:00
|
|
|
test('error event on file stream (reemitErrorEvents=true)', function (t) {
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'error-event-2',
|
2016-02-21 21:31:57 +00:00
|
|
|
streams: [ {
|
2016-02-21 02:04:47 +00:00
|
|
|
path: BOGUS_PATH,
|
|
|
|
reemitErrorEvents: true
|
2016-02-21 21:31:57 +00:00
|
|
|
} ]
|
2016-02-21 02:04:47 +00:00
|
|
|
});
|
|
|
|
log.on('error', function (err, stream) {
|
|
|
|
t.ok(err, 'got err in error event: ' + err);
|
|
|
|
t.equal(err.code, 'ENOENT', 'error code is ENOENT');
|
|
|
|
t.ok(stream, 'got a stream argument');
|
|
|
|
t.equal(stream.path, BOGUS_PATH);
|
|
|
|
t.equal(stream.type, 'file');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.info('info log message');
|
|
|
|
});
|
2016-02-11 07:38:23 +00:00
|
|
|
|
2016-02-21 02:04:47 +00:00
|
|
|
test('error event on file stream (reemitErrorEvents=false)',
|
|
|
|
function (t) {
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'error-event-3',
|
2016-02-21 21:31:57 +00:00
|
|
|
streams: [ {
|
2016-02-21 02:04:47 +00:00
|
|
|
path: BOGUS_PATH,
|
|
|
|
reemitErrorEvents: false
|
2016-02-21 21:31:57 +00:00
|
|
|
} ]
|
2016-02-21 02:04:47 +00:00
|
|
|
});
|
|
|
|
// Hack into the underlying created file stream to catch the error event.
|
|
|
|
log.streams[0].stream.on('error', function (err) {
|
|
|
|
t.ok(err, 'got error event on the file stream');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.on('error', function (err, stream) {
|
|
|
|
t.fail('should not have gotten error event on logger');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.info('info log message');
|
|
|
|
});
|
2016-02-11 07:38:23 +00:00
|
|
|
|
|
|
|
|
2016-02-21 02:04:47 +00:00
|
|
|
function MyErroringStream() {}
|
|
|
|
util.inherits(MyErroringStream, EventEmitter);
|
2016-02-11 07:38:23 +00:00
|
|
|
MyErroringStream.prototype.write = function (rec) {
|
|
|
|
this.emit('error', new Error('boom'));
|
|
|
|
}
|
|
|
|
|
2016-02-21 02:04:47 +00:00
|
|
|
test('error event on raw stream (reemitErrorEvents=undefined)', function (t) {
|
|
|
|
var estream = new MyErroringStream();
|
2016-02-11 07:38:23 +00:00
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'error-event-raw',
|
|
|
|
streams: [
|
|
|
|
{
|
2016-02-21 02:04:47 +00:00
|
|
|
stream: estream,
|
2016-02-11 07:38:23 +00:00
|
|
|
type: 'raw'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
2016-02-21 02:04:47 +00:00
|
|
|
estream.on('error', function (err) {
|
|
|
|
t.ok(err, 'got error event on the raw stream');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.on('error', function (err, stream) {
|
|
|
|
t.fail('should not have gotten error event on logger');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.info('info log message');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('error event on raw stream (reemitErrorEvents=false)', function (t) {
|
|
|
|
var estream = new MyErroringStream();
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'error-event-raw',
|
|
|
|
streams: [
|
|
|
|
{
|
|
|
|
stream: estream,
|
|
|
|
type: 'raw',
|
|
|
|
reemitErrorEvents: false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
estream.on('error', function (err) {
|
|
|
|
t.ok(err, 'got error event on the raw stream');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.on('error', function (err, stream) {
|
|
|
|
t.fail('should not have gotten error event on logger');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
log.info('info log message');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('error event on raw stream (reemitErrorEvents=true)', function (t) {
|
|
|
|
var estream = new MyErroringStream();
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'error-event-raw',
|
|
|
|
streams: [
|
|
|
|
{
|
|
|
|
stream: estream,
|
|
|
|
type: 'raw',
|
|
|
|
reemitErrorEvents: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
2016-02-11 07:38:23 +00:00
|
|
|
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');
|
|
|
|
});
|