Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular reference with JSON format causes an unhandled exception #1348

Closed
1 of 2 tasks
kingjerod opened this issue May 31, 2018 · 2 comments
Closed
1 of 2 tasks

Circular reference with JSON format causes an unhandled exception #1348

kingjerod opened this issue May 31, 2018 · 2 comments

Comments

@kingjerod
Copy link

Please tell us about your environment:

  • winston version?_
  • node -v outputs: v10.1.0
  • Operating System: Linux (Official Docker image)
  • Language: TypeScript 2.6.2

What is the problem?

Logging a variable with a circular reference causes an unhandled exception.

What do you expect to happen instead?

I would expect the exception would be caught and a warning message printed. Or the circular reference would be noted in the resulting JSON, like util.inspect does. Something like:
https://www.npmjs.com/package/circular-json

Other information

Here is the code:

const log = createLogger({
  format: format.json(),
  transports: [new transports.Console()]
});

const a = {name: "A"};
const b = {name: "B", a: a};
a["b"] = b;
log.info("B:", b);

Error Message:

/app/node_modules/logform/json.js:23
  info[MESSAGE] = JSON.stringify(info, opts.replacer || replacer, opts.space);
                       ^

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at Format.module.exports.format [as transform] (/app/node_modules/logform/json.js:23:24)
    at Format.info [as transform] (/app/node_modules/logform/combine.js:20:24)
    at DerivedLogger._transform (/app/node_modules/winston/lib/winston/logger.js:220:27)
    at DerivedLogger.Transform._read (_stream_transform.js:190:10)
    at DerivedLogger.Transform._write (_stream_transform.js:178:12)
    at doWrite (_stream_writable.js:413:12)
    at writeOrBuffer (_stream_writable.js:397:5)
    at DerivedLogger.Writable.write (_stream_writable.js:295:11)
    at DerivedLogger.log (/app/node_modules/winston/lib/winston/logger.js:176:10)
@indexzero
Copy link
Member

indexzero commented Jun 1, 2018

Duplicate of #1248. The reasoning behind this behavior is outlined here: #1248 (comment). TL;DR? Write your own format which handles circular JSON.

@indexzero
Copy link
Member

FYI circular JSON is supported by default thanks to winstonjs/logform#35

@winstonjs winstonjs locked as resolved and limited conversation to collaborators Jun 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants