logdna / logger-node

A nodejs logger client for LogDNA
MIT License
34 stars 17 forks source link

ReferenceError: err is not defined / error stack not included in meta payload #48

Closed strixy closed 3 years ago

strixy commented 3 years ago

I understand why I am receiving this error and I'm happy to provide an explanation (and a PR) just in case anyone else happens to run into it (unlikely as that may be). This error, however, spawned another issue and I hope someone can help me sort that one out.

First issue is related to the example in the readme.md file under the heading Usage. ReferenceError: err is not defined

const logdna = require('@logdna/logger')

const options = {
  app: 'myAppName'
, level: 'debug' // set a default for when level is not provided in function calls
}

const logger = logdna.createLogger('<YOUR INGESTION KEY>', options)

logger.log('This is an INFO statement', 'info')

logger.log('This will be a DEBUG statement, based on the default')

logger.log('This is an INFO statement with an options object', {
  level: 'info'
})

logger.info('This is an INFO statement using a convenience method')

logger.error('This is an error with meta data attached', {
  indexMeta: true
, meta: {
    message: 'TypeError for XYZ'
  , err   <---- ERROR is here because err isn't defined above. Which is fine, sort of.
  }
})

I assumed err was a reference to the error in a catch(err) block, for example, like so....

try {
  nonExistentFunction(42);
} catch (err) {
  logger.error(`This should throw a "ReferenceError: nonExistentFunction is not defined"`, {
    indexMeta: true,
    meta: {
      message: 'TypeError for XYZ',
      err
    }
  });
}

So now the original error is gone, which is expected, but when I check the logs in the web interface, there's no stack. Example.

image

I'm not sure why?

I suppose the main question is, is it possible to send the stack along with the logger in some way?

Also, if this is the correct implementation of err in meta, do you accept PR's?

darinspivey commented 3 years ago

Ah, sorry for the confusion. The docs could be clearer about the example in regards to context. Yes, err is undefined if you copy/paste the example as-is. The docs assume that the user would have already trapped an error in the err variable, but it left that detail out for simplicity in the docs. We can update the docs to have better examples.

This brings up a good point with regards to logging errors, though. Error objects do not serialize well due to some non-enumerable properties. This is basically the answer to the second part of your question. Since meta ultimately calls JSON.stringify before sending data over the wire, the error properties will be lost unless the user specifies them explicitly. We will update the docs to show how to effectively log a real Error, and in the future, we can potentially add auto-detection for serializing Error instance properties automatically.

darinspivey commented 3 years ago

If you use the example from the open PR, the logs will come out looking something like this:

Screen Shot 2021-04-12 at 10 32 35 AM

logdnabot commented 3 years ago

:tada: This issue has been resolved in version 2.3.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: