Error objects inside object or array are discarded during logging
See original GitHub issuee.g. not working
// err object was created by new Error('failed')
winston.error('An error happened:', { error: err, bar: "foo" });
// => An error happened: { error: {}, bar: "foo" }
winston.error('An error happened:', { errors: [err, err, err] });
// => An error happened: { errors: [{},{},{}] }
works well, if top level is Error
itself
winston.error('An error happened:', err);
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:6
Top Results From Across the Web
JavaScript console.log showing Object as Array?
I am using Firebase Auth to create users and logging the error so that I can display it to the user.
Read more >5 Ways to Log an Object to the Console in JavaScript
Here are 5 ways to log JavaScript objects directly to the console window ... Each element in the array (or enumerable property if...
Read more >Array.prototype.splice() - JavaScript - MDN Web Docs
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Read more >JavaScript Arrays - tips, tricks and examples - CodinGame
Arrays are just regular objects. In Javascript, there are only 6 data types defined – the primitives (boolean, number, string, null, undefined) and...
Read more >Destructuring assignment - The Modern JavaScript Tutorial
Arrays allow us to gather data items into an ordered list. Although, when we pass those to a function, it may need not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was facing the same issue so i wrote this package, utils-deep-clone. Check it out.
P.S. I didn’t do it in
winston
itself because i saw the PR’s section and i guess it’s a dead project.I got the same issue - it seems to be caused by the naive cloning done in the
cycle
library’sdecycle
function (https://github.com/dscape/cycle/blob/ab67ce90e8fa2efdb8f21074661366ec56f6a724/cycle.js#L91) -Error
properties are non-enumerable and so nothing gets copied into the “clone” (it returns{}
).This is called from within
winston
to process the metadata on each log call here - https://github.com/winstonjs/winston/blob/de69bccf957cca2247ef529c2b66c88059adbae6/lib/winston/common.js#L142 .It would seem like an upstream PR to
cycle
would be the correct fix, either to iterate non-enumerable properties or perform some other more robust form of cloning. If you just wanted to fix it for your usage, a rewriter can do the job - you have to essentially ‘pre-clone’ the error objects before they get passed tocycle
.Here’s a crude rewriter using
lodash
3.x
’smerge
(mergeWith
in4.x
) with a customiser which addresses the issue:Hopefully that might be of some use to anyone else who gets the same problem.