From 9dfcc20bd91d2fad3abde0595b7748948ca02416 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 31 Mar 2016 14:08:27 -0400 Subject: [PATCH] Use JSON.stringify to log objects There are two major advantages to JSON.stringify over util.inspect: 1.) JSON.stringify defaults to recursively printing deeply nested objects. 2.) JSON.stringify output is JSON, meaning it can be taken directly from the output and used wherever JSON is accepted. util.inspect output is JSON-like, but also includes other annotation such as the types of various values, as well as functions on objects. --- src/lib/log.js | 2 +- test/unit/specs/log.js | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib/log.js b/src/lib/log.js index 664ea3b14..64c0692c2 100644 --- a/src/lib/log.js +++ b/src/lib/log.js @@ -182,7 +182,7 @@ Log.parseLevels = function (input) { Log.join = function (arrayish) { return _.map(arrayish, function (item) { if (_.isPlainObject(item)) { - return _.inspect(item) + '\n'; + return JSON.stringify(item, null, 2) + '\n'; } else { return item.toString(); } diff --git a/test/unit/specs/log.js b/test/unit/specs/log.js index 8976c01bd..345938864 100644 --- a/test/unit/specs/log.js +++ b/test/unit/specs/log.js @@ -97,7 +97,13 @@ describe('Log class', function () { expect(Log.join(['foo', 'bar'])).to.eql('foo bar'); }); it('stringifies objects', function () { - expect(Log.join([{ foo: 'bar' }])).to.eql('{ foo: \'bar\' }\n'); + expect(Log.join([{ foo: 'bar' }])).to.eql('{\n "foo": "bar"\n}\n'); + }); + + it('fully stringifies deeply nested objects', function() { + var object = { foo: { bar: { baz: 'value' } } }; + var expected = '{\n "bar": {\n "baz": "value"\n }\n}\n'; + expect(Log.join(object)).to.eql(expected); }); });