【问题标题】:AWS Lambda Node error logging: Is it necessary to separately log the stack?AWS Lambda 节点错误记录:是否需要单独记录堆栈?
【发布时间】:2020-03-23 04:24:53
【问题描述】:

AWS Javascript SDK 文档中的一些示例代码将错误堆栈与错误本身分开记录,例如来自AWS.CloudWatch overview

var cloudwatch = new AWS.CloudWatch();
cloudwatch.getMetricWidgetImage(params, function (err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

据我所知,这在 Node 中是多余的(我只在 v10.16 上测试过)。如果我只记录错误对象,它包括堆栈跟踪:

> console.log(e)
Error: x
    at repl:2:8
    at Script.runInThisContext (vm.js:122:20)
    at REPLServer.defaultEval (repl.js:332:29)
    at bound (domain.js:402:14)
    at REPLServer.runBound [as eval] (domain.js:415:12)
    at REPLServer.onLine (repl.js:642:10)
    at REPLServer.emit (events.js:198:13)
    at REPLServer.EventEmitter.emit (domain.js:448:20)
    at REPLServer.Interface._onLine (readline.js:308:10)
    at REPLServer.Interface._normalWrite (readline.js:451:12)
undefined

如果我同时记录两者,就像亚马逊上面所做的那样,我会得到一个带有重复的丑陋输出:

> console.log(e, e.stack)
Error: x
    at repl:2:8
    at Script.runInThisContext (vm.js:122:20)
    at REPLServer.defaultEval (repl.js:332:29)
    at bound (domain.js:402:14)
    at REPLServer.runBound [as eval] (domain.js:415:12)
    at REPLServer.onLine (repl.js:642:10)
    at REPLServer.emit (events.js:198:13)
    at REPLServer.EventEmitter.emit (domain.js:448:20)
    at REPLServer.Interface._onLine (readline.js:308:10)
    at REPLServer.Interface._normalWrite (readline.js:451:12) 'Error: x\n    at repl:2:8\n    at Script.runInThisContext (vm.js:122:20)\n    at REPLServer.defaultEval (repl.js:332:29)\n    at bound (domain.js:402:14)\n    at REPLServer.runBound [as eval] (domain.js:415:12)\n    at REPLServer.onLine (repl.js:642:10)\n    at REPLServer.emit (events.js:198:13)\n    at REPLServer.EventEmitter.emit (domain.js:448:20)\n    at REPLServer.Interface._onLine (readline.js:308:10)\n    at REPLServer.Interface._normalWrite (readline.js:451:12)'
undefined

如果我只是使用console.log(err),那么亚马逊使用console.log(err, err.stack) 模式是否有某种原因?

(我主要在 AWS Lambda 中使用 Node,版本 10.x)

【问题讨论】:

标签: node.js amazon-web-services logging


【解决方案1】:

Node 版本 console.log(err, err.stack) 公式,因为它们默认不记录错误堆栈。

最近的 Node 版本可以console.log(err)

更多详情请参阅https://stackoverflow.com/a/33593443/1695906

(在撰写本文时,AWS Lambda 提供的最古老的节点是 Node.js 8.10,因此也许 AWS 现在应该更新他们的文档以删除它。)

(来自“@Michael - sqlbot”的评论,谢谢!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2018-12-21
    • 2017-07-05
    • 2011-04-06
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多