pull #21: some style tweaks, add stream to error event args, test case, example, changelog

master
Trent Mick 2012-06-04 23:13:50 -07:00
parent 63711a654a
commit 858150bbd2
4 changed files with 80 additions and 7 deletions

View File

@ -1,6 +1,15 @@
# bunyan Changelog
## bunyan 0.7.1 (not yet released)
## bunyan 0.8.0 (not yet released)
- [pull #21] Bunyan loggers now re-emit `fs.createWriteStream` error events.
By github.com/EvanOxfeld. See "examples/handle-fs-error.js" and
"test/error-event.js" for details.
var log = new Logger({name: 'mylog', streams: [{path: FILENAME}]});
log.on('error', function (err, stream) {
// Handle error writing to or creating FILENAME.
});
- jsstyle'ing (via `make check`)

View File

@ -0,0 +1,37 @@
// Example handling as fs error for a Bunyan-created
// stream.
var fs = require('fs');
var path = require('path');
var Logger = require('../lib/bunyan');
var FILENAME = 'handle-fs-error.log';
var S_IWUSR = 00200; // mask for owner write permission in stat mode
console.warn('- Log file is "%s".', FILENAME);
if (!path.existsSync(FILENAME)) {
console.warn('- Touch log file.');
fs.writeFileSync(FILENAME, 'touch\n');
}
if (fs.statSync(FILENAME).mode & S_IWUSR) {
console.warn('- Make log file read-only.');
fs.chmodSync(FILENAME, 0444);
}
console.warn('- Create logger.')
var log = new Logger({name: 'handle-fs-error', streams: [{path: FILENAME}]});
log.on('error', function (err) {
console.warn('- The logger emitted an error:', err);
});
console.warn('- Call log.info(...).')
log.info('info log message');
console.warn('- Called log.info(...).')
setTimeout(function () {
console.warn('- Call log.warn(...).')
log.warn('warn log message');
console.warn('- Called log.warn(...).')
}, 1000);

View File

@ -4,7 +4,7 @@
* The bunyan logging library for node.js.
*/
var VERSION = '0.7.1';
var VERSION = '0.8.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,
@ -170,8 +170,6 @@ function resolveLevel(nameOrNum) {
//---- Logger class
Logger.prototype = new EventEmitter;
/**
* Create a Logger instance.
*
@ -242,6 +240,8 @@ function Logger(options, _childOptions, _childSimple) {
throw new TypeError('invalid options.serializers: must be an object')
}
EventEmitter.call(this);
// Fast path for simple child creation.
if (parent && _childSimple) {
// `_isSimpleChild` is a signal to stream close handling that this child
@ -319,9 +319,10 @@ function Logger(options, _childOptions, _childSimple) {
case 'file':
if (!s.stream) {
s.stream = fs.createWriteStream(s.path,
{flags: 'a', encoding: 'utf8'}).on('error', function(err) {
self.emit('error', err);
});
{flags: 'a', encoding: 'utf8'});
s.stream.on('error', function (err) {
self.emit('error', err, s);
});
if (!s.closeOnExit) {
s.closeOnExit = true;
}
@ -404,6 +405,8 @@ function Logger(options, _childOptions, _childSimple) {
});
}
util.inherits(Logger, EventEmitter);
/**
* Create a child logger, typically to add a few log record fields.

24
test/error-event.test.js Normal file
View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2012 Trent Mick. All rights reserved.
*
* Test emission and handling of 'error' event in a logger with a 'path'
* stream.
*/
var test = require('tap').test;
var Logger = require('../lib/bunyan');
test('error event on log write', function (t) {
LOG_PATH = '/this/path/is/bogus.log'
var log = new Logger({name: 'error-event', streams: [{path: LOG_PATH}]});
t.plan(5);
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, LOG_PATH);
t.equal(stream.type, 'file');
t.end();
});
log.info('info log message');
});