我正在创建一个 Javascript 记录器,在错误消息中,我还添加了如下所示的堆栈跟踪:
function logMessage(logMessage)
{
let stackTrace = new Error().stack;
logMessage.stackTrace = stackTrace;
...
}
这给了我堆栈跟踪,但它显然也添加了
logMessage
本身的方法作为堆栈上的最后一项......
如何删除最后一条跟踪,以便我只能看到调用
logMessage
之前的跟踪,但看不到 logMessage
本身?
方法非常简单,因为我们得到的堆栈是一个除以
\n
的字符串,格式如下:
ERROR \n
at ... \n
at ... \n
所以我们需要做的是:
let stackTrace = new Error().stack; //get the stack trace string
let arr = stackTrace.split("\n"); //create an array with all lines
arr.splice(1,1); //remove the second line (first line after "ERROR")
stackTrace = arr.join("\n"); //join array back to a string
堆栈跟踪返回以错误消息开头的多行字符串,然后返回以
at {function name/position}
开头的所有行;
您可以简单地更改多行字符串并使用
at {function name/position}
、split
和 filter
绕过第一次出现的
join
stackTrace.split('\n').filter(function(line, index) { return index !== 1 }).join('\n');
参见片段示例
function deepFunctionStack() {
return new Error().stack;
}
function layer1Function() {
return deepFunctionStack();
}
function topLayerFunction() {
return layer1Function();
}
var originalStack = topLayerFunction();
var formattedStack = originalStack.split('\n').filter(function(line, index) { return index !== 1 }).join('\n');
document.write('Original Stack:');
document.write('<pre>' + originalStack + '</pre>');
document.write('Formatted Stack:');
document.write('<pre>' + formattedStack + '</pre>');
这是一个附加版本,用于处理 Firefox、Chrome 和 IE 堆栈跟踪之间的差异。它将从 Chrome 和 IE 中删除最初的“ERROR”行。它也比此处列出的其他版本快得多。
// stack: string - call stack string
// levels: int - number of levels to remove from the top of the call stack
function trimCallStack(stack, levels) {
if (stack) {
const newLineChar = "\n"; // Each line deliminated by '\n'
const isFirefoxCallStack = stack.indexOf("@") > -1; // If stacktrace contains '@' it is FireFox
// remove an additional line if browser is NOT FireFox (i.e. Chrome or IE) since those start stack trace with the error name
// remove N additional lines specified by `levels`
let iterations = (isFirefoxCallStack ? 0 : 1) + (levels ?? 0);
let start = 0;
while(iterations-- && start !== -1) {
start = stack.indexOf(newLineChar, start + 1);
}
stack = start !== -1 ? stack.substring(start + 1) : ""; // start === -1 if removing more lines than exist, so return "" in that case
}
return stack || "";
}
来自 FireFox 和 Chrome/IE 的堆栈跟踪示例:
Chrome/IE 堆栈跟踪:
Error: fail
at test (<anonymous>:2:8)
at test1 (<anonymous>:5:5)
at test2 (<anonymous>:8:5)
at test3 (<anonymous>:11:5)
at <anonymous>:1:5
Firefox 堆栈跟踪:
test@debugger eval code:2:8
test1@debugger eval code:5:5
test2@debugger eval code:8:5
test3@debugger eval code:11:5
@debugger eval code:1:5
使用提供的功能后:
Chrome/IE 堆栈跟踪:
at test (<anonymous>:2:8)
at test1 (<anonymous>:5:5)
at test2 (<anonymous>:8:5)
at test3 (<anonymous>:11:5)
at <anonymous>:1:5
Firefox 堆栈跟踪:
test@debugger eval code:2:8
test1@debugger eval code:5:5
test2@debugger eval code:8:5
test3@debugger eval code:11:5
@debugger eval code:1:5
我之前使用此问题/答案从堆栈跟踪中删除一个项目以发送到Sentry。
但想向那些为 Sentry 这样做的人指出,这是不需要的。 Sentry 有一种方法可以取消错误堆栈跟踪的优先级
例如,如果我们要删除的功能是
logMessage
,我们会添加
stack.function:captureError -group
https://docs.sentry.io/product/data-management-settings/event-grouping/stack-trace-rules/