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

Uncaught exception when logging object with circular structure #978

Closed
jdoose opened this issue Jan 28, 2017 · 6 comments
Closed

Uncaught exception when logging object with circular structure #978

jdoose opened this issue Jan 28, 2017 · 6 comments
Labels

Comments

@jdoose
Copy link

jdoose commented Jan 28, 2017

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.

@StrongBearCeo
Copy link

Any update on this issue?

@brrian
Copy link

brrian commented 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
});

@StrongBearCeo
Copy link

Thanks @brrian, will try this

@dhensen
Copy link

dhensen commented Apr 6, 2018

For anyone else that comes here: this references 2.x branch not 3.x

@indexzero
Copy link
Member

Development [email protected] has ceased. Please consider upgrading to [email protected]. If you feel strongly about this bug please open a PR against the 2.x branch. Thank you for using winston!

In regards to 3.x please see the response in which you can use a custom format if you wish: #1248 (comment)

@indexzero
Copy link
Member

FYI circular JSON is supported by default in winston@3 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.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants