How to print a stack trace in Node.js?
有人知道如何在node.js中打印堆栈跟踪吗?
任何
1 2 | var stack = new Error().stack console.log( stack ) |
或者更简单地说:
1 | console.trace("Here I am!") |
现在控制台上有一个专门的功能:
1 | console.trace() |
正如已经回答的,您可以简单地使用trace命令:
1 | console.trace("I am here"); |
但是,如果您在搜索如何记录异常的堆栈跟踪时遇到这个问题,您可以简单地记录异常对象。
1 2 3 4 5 6 7 | try { // if something unexpected throw new Error("Something unexpected has occurred."); } catch (e) { console.error(e); } |
它将记录:
Error: Something unexpected has occurred.
at main (c:\Users\Me\Documents\MyApp\app.js:9:15)
at Object. (c:\Users\Me\Documents\MyApp\app.js:17:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
如果node.js版本小于6.0.0,那么记录异常对象就不够了。在这种情况下,它将只打印:
[Error: Something unexpected has occurred.]
对于小于6的节点版本,使用
注意:如果异常创建为类似于
为了安全,您可以使用
1 | console.error(e.stack || e); |
它将适用于旧的和新的node.js版本。
要在控制台中以更可读的方式打印
1 2 | console.log(ex, ex.stack.split(" ")); |
实例结果:
1 2 3 4 5 6 7 8 9 10 11 | [Error] [ 'Error', ' at repl:1:7', ' at REPLServer.self.eval (repl.js:110:21)', ' at Interface. (repl.js:239:12)', ' at Interface.EventEmitter.emit (events.js:95:17)', ' at Interface._onLine (readline.js:202:10)', ' at Interface._line (readline.js:531:8)', ' at Interface._ttyWrite (readline.js:760:14)', ' at ReadStream.onkeypress (readline.js:99:10)', ' at ReadStream.EventEmitter.emit (events.js:98:17)', ' at emitKey (readline.js:1095:12)' ] |
有了现成的节点模块,就可以从节点中获取完整的堆栈跟踪(尽管会受到轻微的性能损失):http://www.mattinsler.com/post/26396305882/announcing-longjohn-long-stack-traces-for-node-js
Try错误。CaptureStackTrace(TargetObject[,ConstructorOpt])。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | const myObj = {}; function c() { // pass } function b() { Error.captureStackTrace(myObj) c() } function a() { b() } a() console.log(myObj.stack) |
函数
据我所知,在nodejs中打印完整的堆栈跟踪是不可能的,您可以只打印一个"部分"堆栈跟踪,您不能从代码中的何处看到您来自何处,只知道异常发生在何处。这就是RyanDahl在YouTube视频中的解释。http://youtu.be/jo_b4lthi3i,精确到56:30。希望这有帮助
如果您只想记录错误的堆栈跟踪(而不是错误消息),则节点6及更高版本会自动将错误名称和消息包含在堆栈跟踪中,如果您想进行自定义错误处理,这有点烦人:
此解决方法将只记录错误名称和堆栈跟踪(例如,您可以格式化错误消息并在代码中的其他地方显示您想要的格式)。
上面的示例将只打印堆栈跟踪后面的错误名称,例如:
1 2 3 4 5 6 7 8 9 10 | Error: at /Users/cfisher/Git/squashed/execProcess.js:6:17 at ChildProcess.exithandler (child_process.js:213:5) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:191:7) at maybeClose (internal/child_process.js:877:16) at Socket. (internal/child_process.js:334:11) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at Pipe._handle.close [as _onclose] (net.js:498:12) |
而不是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Error: Error: Command failed: sh ./commands/getBranchCommitCount.sh HEAD git: 'rev-lists' is not a git command. See 'git --help'. Did you mean this? rev-list at /Users/cfisher/Git/squashed/execProcess.js:6:17 at ChildProcess.exithandler (child_process.js:213:5) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:191:7) at maybeClose (internal/child_process.js:877:16) at Socket. (internal/child_process.js:334:11) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at Pipe._handle.close [as _onclose] (net.js:498:12) |
您可以使用节点堆栈跟踪模块来跟踪调用堆栈,该模块是一个满功率模块。