id0Sch / log4js-json-layout

provides a slim and easy to use json-layout for log4js-node
MIT License
17 stars 8 forks source link

inspect() not working for error objects? #11

Closed jmbldwn closed 3 years ago

jmbldwn commented 3 years ago

I also noticed error objects are spitting out incorrect messages.

This is evident just by running mocha:

  1) log4js-json-layout should format stack traces:
     AssertionError: expected '{"startTime":"615 Ludlam Place, Nicholson, New Mexico, 5763","categoryName":"572efdaaa64be9dbc56369ae","level":"INFO","data":"{ inspect: [Function: inspect] }"}' to include '"data":"Error: Whoops!\\n    at Context.<anonymous>'
      at Context.<anonymous> (test/unit/lib/jsonLayout.js:68:19)
      at processImmediate (internal/timers.js:461:21)

The data field is supposed to be the stack trace, but instead it's coming out"data":"{ inspect: [Function: inspect] }".

It would appear something has changed between node and maybe lodash to cause this.

I don't quite get how this is supposed to work. I'd be happy to put together a PR to fix this, but I need to determine root cause first.

Can someone explain how inspect() is supposed to work?

function wrapErrorsWithInspect(items) {
  return _(items).map((item) => {
    if (_.isError(item) && item.stack) {
      return {
        inspect() {
          return `${util.format(item)}\n${item.stack}`;
        },
      };
    } else if (!_.isObject(item)) {
      return item;
    }
    return undefined;
  }).compact().value();
}

function formatLogData(data) {
  return util.format(...wrapErrorsWithInspect(data));
}
id0Sch commented 3 years ago

resolved in 2.2.3 thanks @jmbldwn @bencehornak