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.
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
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: