有人知道如何在Node.js中打印堆栈跟踪吗?
当前回答
获取函数调用者详细信息:
/**
* @typedef {Object} TCallerInfo
* @property {() => string} toString
* @property {string} str Caller error stack line.
* @property {string} file Caller file path.
* @property {number} line Caller line.
* @property {number} col Caller column.
* @property {Error} error Caller error stack instance.
*/
/**
* @returns {TCallerInfo | null}
*/
function getCallerLine() {
const err = new Error();
const stack = err.stack || '';
const callerLine = stack.split(/\n\s*at\s+/g);
if (callerLine.length >= 2) {
const str = callerLine[3];
const [file, line, col] = str
.replace(/^\s*at\s+/, '')
.replace(/^(.*):(\d+):(\d+)$/, '$1|$2|$3')
.split(/\|/g);
const o = {
toString: () => str,
get str() {
return str;
},
get file() {
return file;
},
get line() {
return parseInt(line);
},
get col() {
return parseInt(col);
},
get error() {
return err;
},
};
return o;
} else {
return null;
}
}
用法:
function foo() {
console.info(getCallerLine());
}
foo(); // Prints this line as Caller Line details.
其他回答
获取函数调用者详细信息:
/**
* @typedef {Object} TCallerInfo
* @property {() => string} toString
* @property {string} str Caller error stack line.
* @property {string} file Caller file path.
* @property {number} line Caller line.
* @property {number} col Caller column.
* @property {Error} error Caller error stack instance.
*/
/**
* @returns {TCallerInfo | null}
*/
function getCallerLine() {
const err = new Error();
const stack = err.stack || '';
const callerLine = stack.split(/\n\s*at\s+/g);
if (callerLine.length >= 2) {
const str = callerLine[3];
const [file, line, col] = str
.replace(/^\s*at\s+/, '')
.replace(/^(.*):(\d+):(\d+)$/, '$1|$2|$3')
.split(/\|/g);
const o = {
toString: () => str,
get str() {
return str;
},
get file() {
return file;
},
get line() {
return parseInt(line);
},
get col() {
return parseInt(col);
},
get error() {
return err;
},
};
return o;
} else {
return null;
}
}
用法:
function foo() {
console.info(getCallerLine());
}
foo(); // Prints this line as Caller Line details.
现在主机上有一个专门的功能:
console.trace()
在控制台以更易读的方式打印错误的堆栈跟踪:
console.log(ex, ex.stack.split("\n"));
结果示例:
[Error] [ 'Error',
' at repl:1:7',
' at REPLServer.self.eval (repl.js:110:21)',
' at Interface.<anonymous> (repl.js:239:12)',
' at Interface.EventEmitter.emit (events.js:95:17)',
' at Interface._onLine (readline.js:202:10)',
' at Interface._line (readline.js:531:8)',
' at Interface._ttyWrite (readline.js:760:14)',
' at ReadStream.onkeypress (readline.js:99:10)',
' at ReadStream.EventEmitter.emit (events.js:98:17)',
' at emitKey (readline.js:1095:12)' ]
任何Error对象都有一个堆栈成员,该成员捕获了构造它的点。
var stack = new Error().stack
console.log( stack )
或者更简单地说:
console.trace("Here I am!")
在v15.12.0中,有各种方法可以做到这一点,
1. console.trace(anything)
2. Error.captureStackTrace(Object)
3. console.log(new Error().stack)
4. Try Catch - Use console.log(e), where `e` is catched by catch block
或者更好地使用 在任何Javascript代码中的stacktracejs
推荐文章
- JavaScript: override alert()
- 重置setTimeout
- 如何确保<select>表单字段被禁用时提交?
- jQuery有不聚焦的方法吗?
- 反应钩子-正确的方式清除超时和间隔
- TypeScript枚举对象数组
- 在React.js中正确的img路径
- 在React.js中更新组件onScroll的样式
- onClick ReactJS调用多个函数
- 如何在JavaScript中转义单引号(')?
- Ng-repeat结束事件
- 谷歌MAP API未捕获的类型错误:无法读取属性“offsetWidth”为空
- 模糊vs聚焦-有什么真正的区别吗?
- 如何使用JavaScript创建和样式一个div ?
- 清除JavaScript中的缓存