This question is similar in nature to Converting an object to a string
console.log works awfully good. It can log very deep objects, self-referencing objects, nulls, undefined, multiparams...
How can I store the output of the function as a string? I want to have my own logging system, but I want to keep the same format as console.log.
The most voted answer for the referenced question does not work with self referencing objects. Nor does .toString():
> a = {}
{}
> a.b = a
<ref *1> { b: [Circular *1] }
> a
<ref *1> { b: [Circular *1] }
> a.b
<ref *1> { b: [Circular *1] }
> JSON.stringify(a)
Uncaught TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
--- property 'b' closes the circle
at JSON.stringify (<anonymous>)
> console.log(a)
<ref *1> { b: [Circular *1] }
undefined
> a.toString()
'[object Object]'
Basically I would want some function stringify
with
> stringify(a) === '<ref *1> { b: [Circular *1] }'
The question is also similar to How can I print a circular structure in a JSON-like format? but not exactly. The question there is to log one self referencing object. I'm asking for something more generic, which is something like the console output. In particular, console handles gracefully multiparams