Winston fails to serialize exceptions to log output #138

Closed
exortech opened this Issue Jun 11, 2012 · 4 comments

Comments

Projects
None yet
2 participants

Winston 0.6.1


var logger = require('winston');
logger.warn('test', { bar: 'baz' });
warn: test bar=baz

logger.warn('test', new Error('foo'));

warn: test

For some reason, Winston fails to serialize the contents of the exception to the log output. This fails regardless of whether the json format is used or not. It fails with the Console and File transports. Serializing javascript objects, arrays, strings, etc. all seem to work fine; however, instances of Error do not.

Contributor

jfhbrook commented Jun 11, 2012

Errors in v8 have non-enumerable values, which means most of your basic object serialization stuff stops working. If you think this is bad, try sending error objects over JSON (doesn't really work).

I believe there are utils in https://github.com/flatiron/errs (particularly merge) that can help with this, but don't hold me to it.

I'm going to close this as a wontfix since really it's a property of Errors and not really winston's fault.

@jfhbrook jfhbrook closed this Jun 11, 2012

Thanks for your quick response. I wasn't aware of errs, and it looks like errs.merge does the trick of nicely outputting the content of an exception.

That said, while I can certainly see the problems with trying to rely on basic object serialization, it would be nice if winston at least did an err.stack on instanceof Error instead of suppressing the output. Logging Errors should be a pretty common occurrence.

It looks like this can be handled using a rewriter. Eg:

logger.addRewriter(function (level, msg, meta) { return (meta instanceof Error) ? meta.stack : meta; });

Unfortunately, the addRewriter method for the default logger is not exposed in the winston namespace. I'll add a separate issue for that.

Contributor

jfhbrook commented Jun 12, 2012

Yeah, using a rewriter sounds reasonable. I'm also hoping to get winston loggers to use util.format by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment