Winston: Uncaught exception when logging object with circular structure

Created on 28 Jan 2017  路  6Comments  路  Source: winstonjs/winston

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.

2.x only

Most helpful comment

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

All 6 comments

Any update on this issue?

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

Thanks @brrian, will try this

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

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: https://github.com/winstonjs/winston/issues/1248#issuecomment-379310813

FYI circular JSON is supported by default in winston@3 thanks to https://github.com/winstonjs/logform/pull/35

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kjin picture kjin  路  3Comments

pocesar picture pocesar  路  3Comments

exortech picture exortech  路  3Comments

Buzut picture Buzut  路  3Comments

JaehyunLee-B2LiNK picture JaehyunLee-B2LiNK  路  3Comments