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

前端 未结 8 875
情话喂你
情话喂你 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:32

    I'm not sure about firefox, but in v8/chrome you can use a method on the Error constructor called captureStackTrace. (More info here)

    So a hacky way to get it would be:

    var getStackTrace = function() {
      var obj = {};
      Error.captureStackTrace(obj, getStackTrace);
      return obj.stack;
    };
    
    console.log(getStackTrace());
    

    Normally, getStackTrace would be on the stack when it's captured. The second argument there excludes getStackTrace from being included in the stack trace.

提交回复
热议问题