test cases for #182, style tweaks, changelog, etc.

This commit is contained in:
Trent Mick 2015-01-18 23:27:28 -08:00
parent c0ca774238
commit 64b8fd1004
9 changed files with 31 additions and 18 deletions

View file

@ -19,3 +19,4 @@ Mihai Tomescu (https://github.com/matomesc)
Daniel Juhl (https://github.com/danieljuhl) Daniel Juhl (https://github.com/danieljuhl)
Chris Barber (https://github.com/cb1kenobi) Chris Barber (https://github.com/cb1kenobi)
Manuel Schneider (https://github.com/manuelschneider) Manuel Schneider (https://github.com/manuelschneider)
Martin Gausby (https://github.com/gausby)

View file

@ -8,7 +8,9 @@ Known issues:
## bunyan 1.3.2 (not yet released) ## bunyan 1.3.2 (not yet released)
(nothing yet) - [pull #182] Fallback to using the optional 'safe-json-stringify' module
if `JSON.stringify` throws -- possibly with an enumerable property
getter than throws. By Martin Gausby.
## bunyan 1.3.1 ## bunyan 1.3.1

View file

@ -43,6 +43,9 @@ try {
} catch (e) { } catch (e) {
safeJsonStringify = null; safeJsonStringify = null;
} }
if (process.env.BUNYAN_TEST_NO_SAFE_JSON_STRINGIFY) {
safeJsonStringify = null;
}
// The 'mv' module is required for rotating-file stream support. // The 'mv' module is required for rotating-file stream support.
try { try {
@ -157,11 +160,16 @@ function getCaller3Info() {
} }
function _indent(s, indent) {
if (!indent) indent = ' ';
var lines = s.split(/\r?\n/g);
return indent + lines.join('\n' + indent);
}
/** /**
* Warn about an bunyan processing error. * Warn about an bunyan processing error.
* *
* If file/line are given, this makes an attempt to warn on stderr only once.
*
* @param msg {String} Message with which to warn. * @param msg {String} Message with which to warn.
* @param dedupKey {String} Optional. A short string key for this warning to * @param dedupKey {String} Optional. A short string key for this warning to
* have its warning only printed once. * have its warning only printed once.
@ -822,17 +830,19 @@ Logger.prototype._emit = function (rec, noemit) {
if (noemit || this.haveNonRawStreams) { if (noemit || this.haveNonRawStreams) {
try { try {
str = JSON.stringify(rec, safeCycles()) + '\n'; str = JSON.stringify(rec, safeCycles()) + '\n';
} } catch (e) {
catch(err) {
if (safeJsonStringify) { if (safeJsonStringify) {
str = safeJsonStringify(rec) + '\n'; str = safeJsonStringify(rec) + '\n';
} } else {
else { var dedupKey = e.stack.split(/\n/g, 2).join('\n');
str = '(Could not JSON stringify data. See stderr for details.)'; _warn('bunyan: ERROR: Exception in '
_warn(format('bunyan: ERROR: Could not JSON stringify a property. ' + '`JSON.stringify(rec)`. You can install the '
+ 'Please consider installing safe-json-stringify for more ' + '"safe-json-stringify" module to have Bunyan fallback '
+ 'information. Stack trace: %s' + 'to safer stringification. Record:\n'
, err.stack)); + _indent(format('%s\n%s', util.inspect(rec), e.stack)),
dedupKey);
str = format('(Exception in JSON.stringify(rec): %j. '
+ 'See stderr for details.)\n', e.message);
} }
} }
} }

View file

@ -20,7 +20,7 @@
"optionalDependencies": { "optionalDependencies": {
"dtrace-provider": "~0.3 >0.3.0", "dtrace-provider": "~0.3 >0.3.0",
"mv": "~2", "mv": "~2",
"safe-json-stringify": "1.0.1" "safe-json-stringify": "~1"
}, },
"devDependencies": { "devDependencies": {
"nodeunit": "0.9.*", "nodeunit": "0.9.*",

View file

@ -6,6 +6,5 @@ var log = bunyan.createLogger({
var obj = {}; var obj = {};
obj.__defineGetter__('boom', obj.__defineGetter__('boom',
function() { throw new Error('__defineGetter__ ouch!'); }); function () { throw new Error('__defineGetter__ ouch!'); });
log.info({obj: obj}, 'using __defineGetter__'); log.info({obj: obj}, 'using __defineGetter__');

View file

@ -7,5 +7,5 @@ var log = bunyan.createLogger({
var obj = {}; var obj = {};
obj.__defineGetter__('boom', obj.__defineGetter__('boom',
function() { throw new Error('__defineGetter__ ouch!'); }); function () { throw new Error('__defineGetter__ ouch!'); });
log.info({obj: obj}, 'using __defineGetter__'); log.info({obj: obj}, 'using __defineGetter__');

View file

@ -7,7 +7,7 @@ var log = bunyan.createLogger({
// And using `Object.defineProperty`. // And using `Object.defineProperty`.
var obj = {}; var obj = {};
Object.defineProperty(obj, 'boom', { Object.defineProperty(obj, 'boom', {
get: function() { throw new Error('defineProperty ouch!'); }, get: function () { throw new Error('defineProperty ouch!'); },
enumerable: true // enumerable is false by default enumerable: true // enumerable is false by default
}); });
// Twice to test the 'warnKey' usage. // Twice to test the 'warnKey' usage.

View file

@ -8,7 +8,7 @@ var log = bunyan.createLogger({
// And using `Object.defineProperty`. // And using `Object.defineProperty`.
var obj = {}; var obj = {};
Object.defineProperty(obj, 'boom', { Object.defineProperty(obj, 'boom', {
get: function() { throw new Error('defineProperty ouch!'); }, get: function () { throw new Error('defineProperty ouch!'); },
enumerable: true // enumerable is false by default enumerable: true // enumerable is false by default
}); });
// Twice to test the 'warnKey' usage. // Twice to test the 'warnKey' usage.

View file

@ -62,6 +62,7 @@ test('defineProperty boom, without safe-json-stringify', function (t) {
t.ok(stderr.indexOf( t.ok(stderr.indexOf(
'You can install the "safe-json-stringify" module') !== -1); 'You can install the "safe-json-stringify" module') !== -1);
t.equal(stderr.match( t.equal(stderr.match(
/* JSSTYLED */
/You can install the "safe-json-stringify" module/g).length, 1); /You can install the "safe-json-stringify" module/g).length, 1);
t.end(); t.end();
}); });