Skip to content

Feature suggestion: default replacer to better handle Error objects passed as meta #31

Description

@mfogel

Hi, thanks a bunch for this project. Super helpful.

Right now, when passing Error objects in the meta parameter they get logged as {} because of the design of the Error object (as you know doubt know).

const log = require('lambda-log')
const error = new Error('foo')
log.warn('Heads up!', {error})
// {"_logLevel":"warn","msg":"Heads up!","error":{},"_tags":["log","warn"]}

I can use log.options.replacer to get a much better serialization of the Error object (using the error-to-json package here):

const log = require('lambda-log')
const errorToJson = require('error-to-json').default
log.options.replacer = (key, value) => {
  if (value instanceof Error) return errorToJson(value)
  return value
}

const error = new Error('foo')
log.warn('Heads up!', {error})
// {"_logLevel":"warn","msg":"Heads up!","error":{"name":"Error","message":"foo","stack":"Error: foo\n    at REPL9:1:15\n    at Script.runInThisContext (vm.js:120:18)\n    at REPLServer.defaultEval (repl.js:442:29)\n    at bound (domain.js:427:14)\n    at REPLServer.runBound [as eval] (domain.js:440:12)\n    at REPLServer.onLine (repl.js:777:10)\n    at REPLServer.emit (events.js:326:22)\n    at REPLServer.EventEmitter.emit (domain.js:483:12)\n    at REPLServer.Interface._onLine (readline.js:329:10)\n    at REPLServer.Interface._line (readline.js:658:8)"},"_tags":["log","warn"]}

My suggestion is to consider making this the default behavior. Are there any use cases where serializing an Error to {} would be the better behavior?

Thanks again for the project!

Activity

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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions