本文翻译自:How to print a stack trace in Node.js?
有谁知道如何在Node.js中打印堆栈跟踪?
参考:https://stackoom.com/question/CGd0/如何在Node-js中打印堆栈跟踪
通过一个随时可用的Node模块,可以从Node获得全长堆栈跟踪(尽管性能损失很小): http : //www.mattinsler.com/post/26396305882/announcing-longjohn-long-stack -traces换节点-JS
Any Error
object has a stack
member that traps the point at which it was constructed. 任何Error
对象都有一个stack
成员,用于捕获构造它的点。
var stack = new Error().stack
console.log( stack )
or more simply: 或更简单地说:
console.trace("Here I am!")
To print stacktrace of Error
in console in more readable way: 以更易读的方式在控制台中打印Error
堆栈跟踪:
console.log(ex, ex.stack.split("\n"));
Example result: 示例结果:
[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)' ]
As already answered, you can simply use the trace command: 如前所述,您只需使用trace命令:
console.trace("I am here");
However, if you came to this question searching about how to log the stack trace of an exception , you can simply log the Exception object. 但是, 如果您在搜索有关如何记录异常的堆栈跟踪的问题时 ,可以只记录Exception对象。
try {
// if something unexpected
throw new Error("Something unexpected has occurred.");
} catch (e) {
console.error(e);
}
It will log: 它会记录:
Error: Something unexpected has occurred. 错误:发生了意外情况。
at main (c:\\Users\\Me\\Documents\\MyApp\\app.js:9:15) 在main(c:\\ Users \\ Me \\ Documents \\ MyApp \\ app.js:9:15)
at Object. 在对象。 (c:\\Users\\Me\\Documents\\MyApp\\app.js:17:1) (C:\\用户\\我\\文件\\ MyApp的\\ app.js:17:1)
at Module._compile (module.js:460:26) 在Module._compile(module.js:460:26)
at Object.Module._extensions..js (module.js:478:10) 在Object.Module._extensions..js(module.js:478:10)
at Module.load (module.js:355:32) 在Module.load(module.js:355:32)
at Function.Module._load (module.js:310:12) 在Function.Module._load(module.js:310:12)
at Function.Module.runMain (module.js:501:10) 在Function.Module.runMain(module.js:501:10)
at startup (node.js:129:16) 在启动时(node.js:129:16)
at node.js:814:3 在node.js:814:3
If your Node.js version is < than 6.0.0 , logging the Exception object will not be enough. 如果Node.js版本小于6.0.0 ,则记录Exception对象是不够的。 In this case, it will print only: 在这种情况下,它只会打印:
[Error: Something unexpected has occurred.] [错误:发生意外事件。]
For Node version < 6, use console.error(e.stack)
instead of console.error(e)
to print the error message plus the full stack, like the current Node version does. 对于Node版本<6,使用console.error(e.stack)
而不是console.error(e)
来打印错误消息加上完整堆栈,就像当前的Node版本一样。
Note: if the exception is created as a string like throw "myException"
, it's not possible to retrieve the stack trace and logging e.stack
yields undefined . 注意:如果将异常创建为类似throw "myException"
的字符串,则无法检索堆栈跟踪并记录e.stack
产生undefined 。
To be safe, you can use 为了安全起见,您可以使用
console.error(e.stack || e);
and it will work for old and new Node.js versions. 它适用于新旧Node.js版本。
您可以使用node-stack-trace模块,它是一个电源完整模块来跟踪调用堆栈。