Skip to content

Commit

Permalink
Fix for Issue winstonjs#76 - cycles in common.serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
dankelleher committed Mar 23, 2018
1 parent 34c562b commit eb23f0c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/loggly/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ common.loggly = function () {
// Performs simple comma-separated, `key=value` serialization for Loggly when
// logging for non-JSON values.
//
common.serialize = function (obj, key) {
common.serialize = function (obj, key, stack) {
if (stack && stack.includes(obj)) {
return (key ? key + '=' : '') + '<Circular>';
}

var updatedStack = stack ? stack.slice() : [];
updatedStack.unshift(obj);

if (obj === null) {
obj = 'null';
}
Expand All @@ -175,7 +182,7 @@ common.serialize = function (obj, key) {
msg += keys[i] + '=[';

for (var j = 0, l = obj[keys[i]].length; j < l; j++) {
msg += common.serialize(obj[keys[i]][j]);
msg += common.serialize(obj[keys[i]][j], null, updatedStack);
if (j < l - 1) {
msg += ', ';
}
Expand All @@ -184,7 +191,7 @@ common.serialize = function (obj, key) {
msg += ']';
}
else {
msg += common.serialize(obj[keys[i]], keys[i]);
msg += common.serialize(obj[keys[i]], keys[i], updatedStack);
}

if (i < length - 1) {
Expand Down
14 changes: 14 additions & 0 deletions test/common-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ vows.describe('node-loggly/common').addBatch({
"should return a deep clone of the object": function (clone) {
assert.isFalse(this.obj.deep === clone.deep);
}
},

"the serialize() method": {
topic: function () {
this.obj = {
name: 'cyclical',
};

this.obj.cycle = this.obj;
return common.serialize(this.obj);
},
"should handle objects containing cyclical references": function (serialized) {
assert.equal("name=cyclical, cycle=<Circular>", serialized);
}
}
}
}).export(module);

0 comments on commit eb23f0c

Please sign in to comment.