[issue #35] Ensure that an accidental log.info(BUFFER), where BUFFER is a node.js Buffer object, doesn't blow up.

This commit is contained in:
Trent Mick 2012-08-22 15:04:30 -07:00
parent d3dc7dfa4d
commit 121b360b08
3 changed files with 100 additions and 1 deletions

View file

@ -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

View file

@ -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);

68
test/buffer.test.js Normal file
View file

@ -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
// // <https://github.com/joyent/node/issues/3905>. 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();
//});