KyleRoss / node-lambda-log

Basic logging mechanism for Node.js Lambda Functions and other AWS services, which produces logs in JSON format for easier reading through Cloudwatch Logs.
https://lambdalog.dev
MIT License
193 stars 16 forks source link

log.error hides error #59

Closed kaihendry closed 3 years ago

kaihendry commented 3 years ago

What happens?

Error isn't shown with log.error

What were you expecting to happen?

error to be shown with msg context

Steps to reproduce

Any logs, output, examples?

(ins)[hendry@t14s wtf]$ cat index.js
const log = require("lambda-log");
try {
  var milli = new Date().now();
} catch (e) {
  log.error("oh no", e);
}
(ins)[hendry@t14s wtf]$ node index.js
{"_logLevel":"error","msg":"oh no","_tags":[]}

Environment

Operating System: Archlinux Node Version: v16.9.1 Lambda Log Version: ^3.0.0

KyleRoss commented 3 years ago

@kaihendry Thanks for reporting this issue. An error must be passed as the first parameter of log.error() instead of the second, which is metadata.

const log = require("lambda-log");

try {
  var milli = new Date().now();
} catch (error) {
  log.error(error);
}

The metadata (meta for short) argument only takes a plain object. Since an Error is not a plain object, it cannot be injected into the metadata of the log message. Adding support for that would require lambda-log to recursively integrate over the meta object to find any error or error-like objects to convert to a plain object which would add too much overhead to this project.

kaihendry commented 3 years ago

So in order to wrap the error with some more context, what would you suggest?

log.error(error, {message: "more context"})

Or am I missing something? Thank you