mperdeck / jsnlog.js

Tiny JavaScript logging library, simple and well documented. Lots of options to filter logging data.
js.jsnlog.com
Other
130 stars 44 forks source link

Logging objects with cyclic values #34

Closed JanisE closed 7 years ago

JanisE commented 7 years ago

If an object is being logged that contains cyclic references (to itself or to one of its (sub-)properties), you get "TypeError: cyclic object value" (see http://stackoverflow.com/a/9382383/99904).

I would suggest replacing return new StringifiedLogObject(null, actualLogObject, JSON.stringify(actualLogObject)); with return new StringifiedLogObject(null, actualLogObject, JSON.stringify(JSON.decycle(actualLogObject))); to avoid the JS error and actually log something.

mperdeck commented 7 years ago

Thanks for your comment. I found that JSON.decycle is not supported by browsers, but sits in a separate library. So to give yourself and others the option to use this without complicating the library for those who don't need it, I created the option to set your own method for serializing objects.

This is now live (version 2.21.0)

A worked out example that calls decycle is at: http://jsnlog.com/Documentation/HowTo/CyclicReferences