diff --git a/CHANGES.md b/CHANGES.md index eb4cac7..450b4b4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,8 @@ ## bunyan 0.13.2 (not yet released) -(nothing yet) +- [issue #35] Ensure that an accidental `log.info(BUFFER)`, where BUFFER is + a node.js Buffer object, doesn't blow up. ## bunyan 0.13.1 diff --git a/lib/bunyan.js b/lib/bunyan.js index 1983c6c..8c23614 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -728,6 +728,11 @@ Logger.prototype.trace = function () { } else if (typeof (arguments[0]) === 'string') { // `log.trace(msg, ...)` fields = null; msgArgs = Array.prototype.slice.call(arguments); + } else if (Buffer.isBuffer(arguments[0])) { // `log.trace(buf, ...)` + // Almost certainly an error, show `inspect(buf)`. See bunyan issue #35. + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + msgArgs[0] = util.inspect(msgArgs[0]); } else { // `log.trace(fields, msg, ...)` fields = arguments[0]; msgArgs = Array.prototype.slice.call(arguments, 1); @@ -767,6 +772,11 @@ Logger.prototype.debug = function () { } else if (typeof (arguments[0]) === 'string') { // `log.debug(msg, ...)` fields = null; msgArgs = Array.prototype.slice.call(arguments); + } else if (Buffer.isBuffer(arguments[0])) { // `log.debug(buf, ...)` + // Almost certainly an error, show `inspect(buf)`. See bunyan issue #35. + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + msgArgs[0] = util.inspect(msgArgs[0]); } else { // `log.debug(fields, msg, ...)` fields = arguments[0]; msgArgs = Array.prototype.slice.call(arguments, 1); @@ -806,6 +816,11 @@ Logger.prototype.info = function () { } else if (typeof (arguments[0]) === 'string') { // `log.info(msg, ...)` fields = null; msgArgs = Array.prototype.slice.call(arguments); + } else if (Buffer.isBuffer(arguments[0])) { // `log.info(buf, ...)` + // Almost certainly an error, show `inspect(buf)`. See bunyan issue #35. + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + msgArgs[0] = util.inspect(msgArgs[0]); } else { // `log.info(fields, msg, ...)` fields = arguments[0]; msgArgs = Array.prototype.slice.call(arguments, 1); @@ -845,6 +860,11 @@ Logger.prototype.warn = function () { } else if (typeof (arguments[0]) === 'string') { // `log.warn(msg, ...)` fields = null; msgArgs = Array.prototype.slice.call(arguments); + } else if (Buffer.isBuffer(arguments[0])) { // `log.warn(buf, ...)` + // Almost certainly an error, show `inspect(buf)`. See bunyan issue #35. + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + msgArgs[0] = util.inspect(msgArgs[0]); } else { // `log.warn(fields, msg, ...)` fields = arguments[0]; msgArgs = Array.prototype.slice.call(arguments, 1); @@ -884,6 +904,11 @@ Logger.prototype.error = function () { } else if (typeof (arguments[0]) === 'string') { // `log.error(msg, ...)` fields = null; msgArgs = Array.prototype.slice.call(arguments); + } else if (Buffer.isBuffer(arguments[0])) { // `log.error(buf, ...)` + // Almost certainly an error, show `inspect(buf)`. See bunyan issue #35. + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + msgArgs[0] = util.inspect(msgArgs[0]); } else { // `log.error(fields, msg, ...)` fields = arguments[0]; msgArgs = Array.prototype.slice.call(arguments, 1); @@ -923,6 +948,11 @@ Logger.prototype.fatal = function () { } else if (typeof (arguments[0]) === 'string') { // `log.fatal(msg, ...)` fields = null; msgArgs = Array.prototype.slice.call(arguments); + } else if (Buffer.isBuffer(arguments[0])) { // `log.fatal(buf, ...)` + // Almost certainly an error, show `inspect(buf)`. See bunyan issue #35. + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + msgArgs[0] = util.inspect(msgArgs[0]); } else { // `log.fatal(fields, msg, ...)` fields = arguments[0]; msgArgs = Array.prototype.slice.call(arguments, 1); diff --git a/test/buffer.test.js b/test/buffer.test.js new file mode 100644 index 0000000..13c374a --- /dev/null +++ b/test/buffer.test.js @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2012 Trent Mick. All rights reserved. + * Copyright (c) 2012 Joyent Inc. All rights reserved. + * + * Test logging with (accidental) usage of buffers. + */ + +var util = require('util'), + inspect = util.inspect, + format = util.format; +var test = require('tap').test; +var bunyan = require('../lib/bunyan'); + + +function Catcher() { + this.records = []; +} +Catcher.prototype.write = function (record) { + this.records.push(record); +} + +var catcher = new Catcher(); +var log = new bunyan.createLogger({ + name: 'buffer.test', + streams: [ + { + type: 'raw', + stream: catcher, + level: 'trace' + } + ] +}); + + +test('log.info(BUFFER)', function (t) { + var b = new Buffer('foo'); + + ['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(function (lvl) { + log[lvl].call(log, b); + var rec = catcher.records[catcher.records.length - 1]; + t.equal(rec.msg, inspect(b), + format('log.%s msg is inspect(BUFFER)', lvl)); + t.ok(rec["0"] === undefined, + 'no "0" array index key in record: ' + inspect(rec["0"])); + t.ok(rec["parent"] === undefined, + 'no "parent" array index key in record: ' + inspect(rec["parent"])); + + log[lvl].call(log, b, 'bar'); + var rec = catcher.records[catcher.records.length - 1]; + t.equal(rec.msg, inspect(b) + ' bar', + format('log.%s(BUFFER, "bar") msg is inspect(BUFFER) + " bar"', lvl)); + }); + + t.end(); +}); + + +//test('log.info({buf: BUFFER})', function (t) { +// var b = new Buffer('foo'); +// +// // Really there isn't much Bunyan can do here. See +// // . An unwelcome hack would +// // be to monkey-patch in Buffer.toJSON. Bletch. +// log.info({buf: b}, 'my message'); +// var rec = catcher.records[catcher.records.length - 1]; +// +// t.end(); +//});