From 858150bbd21a0a43e965a51c2182bd80fa00b5a6 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Mon, 4 Jun 2012 23:13:50 -0700 Subject: [PATCH] pull #21: some style tweaks, add stream to error event args, test case, example, changelog --- CHANGES.md | 11 ++++++++++- examples/handle-fs-error.js | 37 +++++++++++++++++++++++++++++++++++++ lib/bunyan.js | 15 +++++++++------ test/error-event.test.js | 24 ++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 examples/handle-fs-error.js create mode 100644 test/error-event.test.js diff --git a/CHANGES.md b/CHANGES.md index bfe9d34..96e028f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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`) diff --git a/examples/handle-fs-error.js b/examples/handle-fs-error.js new file mode 100644 index 0000000..517999f --- /dev/null +++ b/examples/handle-fs-error.js @@ -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); + diff --git a/lib/bunyan.js b/lib/bunyan.js index 6b02041..ffcf567 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -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. diff --git a/test/error-event.test.js b/test/error-event.test.js new file mode 100644 index 0000000..6c8f0cf --- /dev/null +++ b/test/error-event.test.js @@ -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'); +});