首页 文章

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

提问于
浏览
424

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

9 回答

  • 70

    要以更易读的方式在控制台中打印 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)' ]
    
  • 34

    现在有一个dedicated function on console

    console.trace()
    
  • 172

    试试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 中 .

  • 0

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

  • 2

    你可以使用node-stack-trace模块,它是一个电源满模块来跟踪调用堆栈 .

  • 511

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

  • 8

    如果您只想记录错误的堆栈跟踪(而不是错误消息),节点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)
    
  • 2

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

    console.trace("I am here");
    

    但是, if you came to this question searching about how to log the stack trace of an exception ,您只需记录Exception对象即可 .

    try {  
      // if something unexpected
      throw new Error("Something unexpected has occurred.");     
    
    } catch (e) {
      console.error(e);
    }
    

    它会记录:

    错误:发生了意外情况 . at对象的main(c:\ Users \ Me \ Documents \ MyApp \ app.js:9:15) . (c:\ Users \ Me \ Documents \ MyApp \ app.js:17:1)在Module._compile(module.js:460:26)处于Object.Module._extensions..js(module.js:478:10) )在Function.Module.rload(module.js:310:12)上的Module.load(module.js:355:32),在启动时在Function.Module.runMain(module.js:501:10)处(node.js) :129:16)在node.js:814:3

    If your Node.js version is < than 6.0.0 ,记录Exception对象是不够的 . 在这种情况下,它只会打印:

    [错误:发生了意外情况 . ]

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

    Note: 如果将异常创建为类似 throw "myException" 的字符串,则无法检索堆栈跟踪并记录 e.stack 产生 undefined .

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

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

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

  • 3

    任何 Error 对象都有一个 stack 成员,用于捕获构造它的点 .

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

    或更简单地说:

    console.trace("Here I am!")
    

相关问题