kel*_*oti 34
您可以.stack从任何错误中获取该属性.例如:
try {
throw new Error();
} catch (e) {
console.log(e.stack);
}
Run Code Online (Sandbox Code Playgroud)
或者只是new为了获得堆栈跟踪而出错
console.log(new Error().stack)
Run Code Online (Sandbox Code Playgroud)
如果你使用winston,你可以添加:
winston = require('winston');
logger = expandErrors(new winston.Logger());
logger.info(new Error("my error"));
// Extend a winston by making it expand errors when passed in as the
// second argument (the first argument is the log level).
function expandErrors(logger) {
var oldLogFunc = logger.log;
logger.log = function() {
var args = Array.prototype.slice.call(arguments, 0);
if (args.length >= 2 && args[1] instanceof Error) {
args[1] = args[1].stack;
}
return oldLogFunc.apply(this, args);
};
return logger;
}
Run Code Online (Sandbox Code Playgroud)
然后你得到带有堆栈跟踪的winston记录器.也是一个要点.
| 归档时间: |
|
| 查看次数: |
18526 次 |
| 最近记录: |