mathew-kurian / Scribe.js

:scroll: Node.js logging made simple! Online access to logs and more...
https://mathew-kurian.github.io/Scribe.js/
MIT License
284 stars 45 forks source link

Nodejs Error objects show as {} in the logs #70

Open LudgerPeters opened 8 years ago

LudgerPeters commented 8 years ago

When i try and log a error it shows up as a empty object {} I found something interesting http://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify Looks like json stringify does not handle errors. I replaced line 405 of console2 to:

msg += delimiter + util.inspect(arg, { showHidden: true, depth: null });

Is there something else i can do to get scribe to log the error messages?

LudgerPeters commented 8 years ago

Another alternative is that you can add the following code at startup and then JSON.stringify works as expected:

Object.defineProperty(Error.prototype, 'message', {
    configurable: true,
    enumerable: true
});
Object.defineProperty(Error.prototype, 'stack', {
    configurable: true,
    enumerable: true
});

credit for the above suggestion goes to cheolgook over at stackoverflow

xfoxfu commented 8 years ago

I think you can use the following codes:

// hack on Error
Object.defineProperty(Error.prototype, 'toJSON', {
  configurable: true,
  value: function () {
    var alt = {};
    var storeKey = function (key) {
      alt[key] = this[key];
    };
    Object.getOwnPropertyNames(this).forEach(storeKey, this);
    return alt;
  }
});
mathew-kurian commented 8 years ago

The alpha release has fixed this problem. Be sure to check it out.