如何在Node.js中打印堆栈跟踪?

问题描述 投票:449回答:9

有谁知道如何在Node.js中打印堆栈跟踪?

javascript node.js stack-trace
9个回答
532
投票

任何Error对象都有一个stack成员,可以捕获它构造的点。

var stack = new Error().stack
console.log( stack )

或更简单地说:

console.trace("Here I am!")

174
投票

现在有一个dedicated function on console

console.trace()

77
投票

如前所述,您只需使用trace命令:

console.trace("I am here");

但是,如果您在搜索有关如何记录异常的堆栈跟踪的问题时,可以只记录Exception对象。

try {  
  // if something unexpected
  throw new Error("Something unexpected has occurred.");     

} catch (e) {
  console.error(e);
}

它会记录:

错误:发生了意外情况。 在main(c:\ Users \ Me \ Documents \ MyApp \ app.js:9:15) 在对象。 (C:\用户\我\文件\ MyApp的\ app.js:17:1) 在Module._compile(module.js:460:26) 在Object.Module._extensions..js(module.js:478:10) 在Module.load(module.js:355:32) 在Function.Module._load(module.js:310:12) 在Function.Module.runMain(module.js:501:10) 在启动时(node.js:129:16) 在node.js:814:3

如果Node.js版本小于6.0.0,则记录Exception对象是不够的。在这种情况下,它只会打印:

[错误:发生意外事件。]

对于节点版本<6,使用console.error(e.stack)而不是console.error(e)来打印错误消息加上完整堆栈,就像当前的Node版本一样。

注意:如果异常是作为像throw "myException"这样的字符串创建的,则无法检索堆栈跟踪并且记录e.stack会产生undefined。

为了安全起见,您可以使用

console.error(e.stack || e);

它适用于新旧Node.js版本。


35
投票

以更易读的方式在控制台中打印Error的堆栈跟踪:

console.log(ex, ex.stack.split("\n"));

示例结果:

[Error] [ 'Error',
  '    at repl:1:7',
  '    at REPLServer.self.eval (repl.js:110:21)',
  '    at Interface.<anonymous> (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)' ]

9
投票

通过一个随时可用的Node模块,可以从Node中获取全长堆栈跟踪(尽管性能损失很小):http://www.mattinsler.com/post/26396305882/announcing-longjohn-long-stack-traces-for-node-js


3
投票

我知道打印nodejs中的完整堆栈跟踪是不可能的,你可以只打印一个“部分”堆栈跟踪,你无法从代码的哪个位置看到异常发生的位置。这就是Ryan Dahl在这个YouTube视频中解释的内容。 http://youtu.be/jo_B4LTHi3I在56:30分钟是准确的。希望这可以帮助


3
投票

试试Error.captureStackTrace(targetObject[, constructorOpt])

const myObj = {};
function c() {
  // pass
}

function b() {
    Error.captureStackTrace(myObj)
    c()
} 

function a() {
    b()
}

a()

console.log(myObj.stack)

函数ab在错误堆栈中捕获并存储在myObj中。


2
投票

如果您只想记录错误的堆栈跟踪(而不是错误消息),节点6及更高版本会自动在堆栈跟踪中包含错误名称和消息,如果您想要进行一些自定义错误处理,这有点烦人:

console.log(error.stack.replace(error.message, ''))

此解决方法将仅记录错误名称和堆栈跟踪(例如,您可以格式化错误消息并在代码中的其他位置显示它的方式)。

上面的示例将仅打印堆栈跟踪后面的错误名称,例如:

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.<anonymous> (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)

代替:

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.<anonymous> (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)

0
投票

您可以使用node-stack-trace模块,它是一个电源完整模块来跟踪调用堆栈。

© www.soinside.com 2019 - 2024. All rights reserved.