Skip to content

Commit

Permalink
Formatters: JSON.stringify object literals in Human
Browse files Browse the repository at this point in the history
  • Loading branch information
TomFrost committed Jul 3, 2014
1 parent 3c96d9b commit 70a9acc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Insanely configurable logging for Node.js

##ChangeLog

### Development HEAD
- Fix: Human formatter now outputs pretty-printed JSON when it encounters an
object literal instead of "[object Object]".

### v0.3.0
- Added: Loggly target
- Added: Better documentation regarding targets
Expand Down
12 changes: 11 additions & 1 deletion lib/formatters/human.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ module.exports = function(options, severity, date, elems) {
delete obj.line;
}
logUtil.forEachObj(obj, function(key, val) {
msg += "\n\t" + key + ': ' + val;
var strVal = '';
try {
strVal = val.toString();
if (strVal === '[object Object]') {
// Pretty-print objects with JSON
strVal = ("\n" + JSON.stringify(val, null, " "))
.replace(/\n/g, "\n\t ");
}
}
catch (e) {}
msg += "\n\t" + key + ': ' + strVal;
});
return msg;
};
9 changes: 7 additions & 2 deletions test/formatters/human.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('Human Formatter', function() {
msg = human({}, 'debug', new Date(), [
'Hello!',
{is_it: 'me', "you're": 'looking for'},
{file: __filename, line: 82}
{file: __filename, line: 82},
{obj: { str: "hello" }}
]);
});
it("should output multiple lines", function() {
Expand All @@ -30,5 +31,9 @@ describe('Human Formatter', function() {
});
it("should output file and line in line 1", function() {
msg.should.match(/^[^\n]+human\.js:82/);
})
});
it("should stringify object literals", function() {
msg.should.not.match(/\[object Object\]/);
msg.should.match(/"str": "hello"/);
});
});

0 comments on commit 70a9acc

Please sign in to comment.