lazywithclass/winston-cloudwatch

Handle circular objects

Closed this issue · 1 comments

Hello!

First of all thank you for the amazing job with this, as it saved me (and my company indeed) several hours of useless development :D

Apparently we have issues with circular data structures. It sounds like an easy fix, as there are lots of libraries catering for these kind of cases, so if you're open for contributions I'd be glad to help here.

Reproducing the issue

... skipping winston setup ...

const circular = {}
const child = { circular }
circular.child = child

logger.info(circular)

Error

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at CloudWatchTransport.stringify [as formatMessage] (***/node_modules/winston-cloudwatch/lib/utils.js:12:37)
    at CloudWatchTransport.WinstonCloudWatch.add (***/node_modules/winston-cloudwatch/index.js:92:21)
    at CloudWatchTransport.WinstonCloudWatch.log (***/node_modules/winston-cloudwatch/index.js:68:10)
    at CloudWatchTransport._write (***/node_modules/winston-transport/index.js:81:19)
    at doWrite (***/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:428:64)
    at writeOrBuffer (***/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:417:5)
    at CloudWatchTransport.Writable.write (***/node_modules/winston-transport/node_modules/readable-stream/lib/_stream_writable.js:334:11)
    at DerivedLogger.ondata (***/node_modules/winston/node_modules/readable-stream/lib/_stream_readable.js:619:20)
    at DerivedLogger.emit (events.js:187:15)
    at DerivedLogger.EventEmitter.emit (domain.js:442:20)
    at addChunk (***/node_modules/winston/node_modules/readable-stream/lib/_stream_readable.js:291:12)
    at readableAddChunk (***/node_modules/winston/node_modules/readable-stream/lib/_stream_readable.js:278:11)
    at DerivedLogger.Readable.push (***/node_modules/winston/node_modules/readable-stream/lib/_stream_readable.js:245:10)
    at DerivedLogger.Transform.push (***/node_modules/winston/node_modules/readable-stream/lib/_stream_transform.js:148:32)
    at DerivedLogger._transform (***/node_modules/winston/lib/winston/logger.js:235:12)
    at DerivedLogger.Transform._read (***/node_modules/winston/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at DerivedLogger.Transform._write (***/node_modules/winston/node_modules/readable-stream/lib/_stream_transform.js:172:83)
    at doWrite (***/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:428:64)
    at writeOrBuffer (***/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:417:5)
    at DerivedLogger.Writable.write (***/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:334:11)
    at LogEventsHandler.(anonymous function).args [as info] (***/node_modules/winston/lib/winston/create-logger.js:54:16)
...

Proposed solution

// ./lib/utils.js

var chalk = require('chalk');
var safeStringify = require('fast-safe-stringify');

...
function stringify(o) { return safeStringify(o, handleErrorObject, '  '); }
...

Hi @shikaan, it's actually far from being amazing, but I thank you nonetheless :D

I am not using this library in production anymore since years (changed job), but I am maintaining it (more or less), so if you're willing to submit a PR you've tested in your environment I am more than happy to review and merge it!

Don't bother updating README, package.json, etc... I will take care of those things.