Javascript 如何使用chrome或firefox在javascript中将console.trace()的结果作为字符串获取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6715571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get result of console.trace() as string in javascript with chrome or firefox?
提问by js_
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 I also can not get their names with callee.caller.name
.
console.trace()
在控制台上输出其结果。
我想以字符串形式获取结果并将它们保存到文件中。
我没有为函数定义名称,也无法使用callee.caller.name
.
回答by chjj
I'm not sure about firefox, but in v8/chrome you can use a method on the Error constructor called captureStackTrace
. (More info here)
我不确定 firefox,但在 v8/chrome 中,您可以在名为captureStackTrace
. (更多信息在这里)
So a hacky way to get it would be:
所以获得它的一种hacky方法是:
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.
通常,getStackTrace
当它被捕获时会在堆栈上。那里的第二个参数排除getStackTrace
在堆栈跟踪中。
回答by Molecular Man
Error.stack is what you need. It works in Chrome and Firefox. For example
Error.stack 正是您所需要的。它适用于 Chrome 和 Firefox。例如
try { var a = {}; a.debug(); } catch(ex) {console.log(ex.stack)}
will give in Chrome:
将在 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:
在 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
回答by Konstantin Smolyanin
This will give a stack trace (as array of strings) for modern Chrome, Firefox, Opera and IE10+
这将为现代 Chrome、Firefox、Opera 和 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).
它从堆栈中排除了它自己的调用以及 Chrome 和 Firefox(但不是 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.
它不应该在旧浏览器上崩溃,而只是返回空数组。如果您需要更通用的解决方案,请查看stacktrace.js。它支持的浏览器列表确实令人印象深刻,但在我看来,它对于它旨在执行的小任务来说非常大:37Kb 的缩小文本,包括所有依赖项。
回答by fijiaaron
There is a library called stacktrace.jsthat gives you cross browser stack traces. You can use it simply by including the script and calling at any point:
有一个名为stacktrace.js的库,可为您提供跨浏览器堆栈跟踪。您可以通过包含脚本并随时调用来使用它:
var trace = printStackTrace();
回答by sgouros
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:
这只是对 Konstantin 优秀代码的一个小改进。它减少了扔接球的开销,只是实例化了错误堆栈:
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
回答by jcubic
you only need var stack = new Error().stack
. this is simplified version of @sgouros answer.
你只需要var stack = new Error().stack
. 这是@sgouros 答案的简化版本。
function foo() {
bar();
}
function bar() {
baz();
}
function baz() {
console.log(new Error().stack);
}
foo();
Probably will not work in every browser (works in Chrome).
可能不适用于所有浏览器(适用于 Chrome)。