How to get result of console.trace() as string in javascript with chrome or firefox?

前端 未结 8 784
情话喂你
情话喂你 2020-12-02 12:31

console.trace() outputs its result on console.
I want to get the results as string and save them to a file.

I don\'t define names for functions and

相关标签:
8条回答
  • 2020-12-02 13:37

    Error.stack is what you need. It works in Chrome and Firefox. For example

    try { var a = {}; a.debug(); } catch(ex) {console.log(ex.stack)}
    

    will give in Chrome:

    TypeError: Object #<Object> has no method 'debug'
        at eval at <anonymous> (unknown source)
        at eval (native)
        at Object._evaluateOn (unknown source)
        at Object._evaluateAndWrap (unknown source)
        at Object.evaluate (unknown source)
    

    and in Firefox:

    @http://www.google.com.ua/:87 _firebugInjectedEvaluate("with(_FirebugCommandLine){try { var a = {}; a.debug() } catch(ex) {console.log(ex.stack)}\n};")
    @http://www.google.com.ua/:87 _firebugEvalEvent([object Event])
    @http://www.google.com.ua/:67
    
    0 讨论(0)
  • 2020-12-02 13:38

    There is a library called stacktrace.js that gives you cross browser stack traces. You can use it simply by including the script and calling at any point:

    var trace = printStackTrace();
    
    0 讨论(0)
提交回复
热议问题