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

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

    This will give a stack trace (as array of strings) for modern Chrome, Firefox, Opera and IE10+

    function getStackTrace () {
    
      var stack;
    
      try {
        throw new Error('');
      }
      catch (error) {
        stack = error.stack || '';
      }
    
      stack = stack.split('\n').map(function (line) { return line.trim(); });
      return stack.splice(stack[0] == 'Error' ? 2 : 1);
    }
    

    Usage:

    console.log(getStackTrace().join('\n'));
    

    It excludes from the stack its own call as well as title "Error" that is used by Chrome and Firefox (but not IE).

    It shouldn't crash on older browsers but just return empty array. If you need more universal solution look at stacktrace.js. Its list of supported browsers is really impressive but to my mind it is very big for that small task it is intended for: 37Kb of minified text including all dependencies.

    0 讨论(0)
  • 2020-12-02 13:23

    I was trying to get Stack Trace as string variable in JavaScript on NodeJS and this tutorial helped me. This will work in your scenario as well except stack trace is printed via Error Object than console.trace().

    Code to print stack trace:

    function add(x, y) {
        console.log(new Error().stack);
        return x+y;
    }
    
    0 讨论(0)
  • 2020-12-02 13:30

    This is only a minor enhancement to Konstantin's excellent code. It cuts a bit on the expense of throwing-catching and just instantiates the Error stack:

    function getStackTrace () {
        let stack = new Error().stack || '';
        stack = stack.split('\n').map(function (line) { return line.trim(); });
        return stack.splice(stack[0] == 'Error' ? 2 : 1);
    }
    

    I usually want a specific level of stack trace (for my custom logger) so this is also possible when calling:

    getStackTrace()[2]; // get stack trace info 2 levels-deep
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 13:33

    you only need var stack = new Error().stack. this is simplified version of @sgouros answer.

    function foo() {
      bar();
    }
    function bar() {
      baz();
    }
    function baz() {
      console.log(new Error().stack);
    }
    
    foo();

    Probably will not work in every browser (works in Chrome).

    0 讨论(0)
  • 2020-12-02 13:35

    console.trace includes async operations. new Error().stack doesn't. I'm still waiting for a better answer.

    0 讨论(0)
提交回复
热议问题