question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught exception when logging object with circular structure

See original GitHub issue

If the option “json” is set to “true” given arguments are converted to JSON. If the argument has circular structures (as it can happen easily in express environments) the formatting command fails with an exception.

This happens for the console transport beginning with line 37

  if (this.json) {
    this.stringify = options.stringify || function (obj) {
      return JSON.stringify(obj, null, 2);
    };
  }

This also happens for the winston-daily-rotate-file transport

IMHO a logger should never fail while formatting the given object. Instead I would prefer if the argument in question would be omitted from logging.

What do you think?

I can provide patches if you agree.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:16
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
brriancommented, Jan 25, 2018

I was able to get around this by specifying a “stringify” param in the options and using a library like circular-json:

const logger = new winston.Logger({
  transports: [
    new (winston.transports.File)({
      filename: 'errors.log',
      level: 'warn',
      handleExceptions: true,
      stringify(obj) {
        return CircularJSON.stringify(obj);
      }
    })
  ],
  exitOnError: false
});

1reaction
StrongBearCeocommented, Nov 27, 2017

Any update on this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Converting circular structure to JSON - Stack ...
TypeError : Converting circular structure to JSON. It seems that I can get the object logged to the console by using just plain:...
Read more >
What is TypeError: Converting circular structure to JSON?
TypeError : Converting circular structure to JSON occurs when you try to reference your variable name within the JSON object.
Read more >
TypeError: cyclic object value - JavaScript - MDN Web Docs
The JavaScript exception "cyclic object value" occurs when object references ... TypeError: Converting circular structure to JSON (V8-based) ...
Read more >
Logging cyclic references - JSNLog
Using the serialize option to log objects with cyclic references. ... an exception, such as: Uncaught TypeError: Converting circular structure to JSON.
Read more >
Converting Circular Structure to JSON - Career Karma
JSON does not support object references, so trying to stringify a JSON object that references itself will result in a typeerror. It is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found