访问V8 JavaScript中的行号(Chrome&Node.js)
Accessing line number in V8 JavaScript (Chrome & Node.js)
在C等语言中花费时间的JavaScript开发人员经常无法使用某些类型的内省,如日志行号,以及从什么方法调用当前方法。如果您使用的是V8(Chrome,Node.js),那么您可以使用以下内容。
Object.defineProperty(global, '__stack', {
get: function(){
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});
Object.defineProperty(global, '__line', {
get: function(){
return __stack[1].getLineNumber();
}
});
console.log(__line);
以上内容将记录19
。
与arguments.callee.caller
相结合,您可以通过宏更接近于在C中获得的有用日志记录类型。
接受的答案IMO的问题是,当你想打印一些东西时,你可能正在使用记录器,在这种情况下,使用接受的解决方案将始终打印同一行:)
一些小的改变将有助于避免这种情况!
在我们的案例中,我们使用Winston进行日志记录,因此代码如下所示(请注意下面的代码注释):
/**
* Use CallSite to extract filename and number, for more info read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
* @returns {string} filename and line number separated by a colon
*/
const getFileNameAndLineNumber = () => {
const oldStackTrace = Error.prepareStackTrace;
try {
// eslint-disable-next-line handle-callback-err
Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace;
Error.captureStackTrace(this);
// in this example I needed to "peel" the first CallSites in order to get to the caller we're looking for
// in your code, the number of stacks depends on the levels of abstractions you're using
// in my code I'm stripping frames that come from logger module and winston (node_module)
const callSite = this.stack.find(line => line.getFileName().indexOf('/logger/') < 0 && line.getFileName().indexOf('/node_modules/') < 0);
return callSite.getFileName() + ':' + callSite.getLineNumber();
} finally {
Error.prepareStackTrace = oldStackTrace;
}
};
相关文章:
- jquery点击函数select&取消选择
- Node.js v6.2.0类扩展不是函数错误
- 如何使用 node.js 比较两个 json 数组
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- Node.js's Buffer.writeFloatBE in Javascript
- node.js:setInterval()正在跳过调用
- Angular js-返回一个包含类似
- 一点javascript元编程&可链接的设置器
- html5拖动&drop-TypeError:Node.appendChild的参数1不是对象
- Node.js&两个请求之间的Express/Can上下文切换可以是同步代码
- node.js+MySQL;JSON结果-回调问题&没有响应客户端
- 访问V8 JavaScript中的行号(Chrome&Node.js)
- Node.js&Javascript/Date对象-在x小时之前花费时间
- Node.js-样式没有't应用于页面&谷歌Chrome表示:未捕获语法错误:意外的令牌<
- 无法使用node.js&有棱角的
- Node.js、Express&Ghost子目录URL解析错误
- Node.js的子进程套接字写入在Chrome&FF
- Node.js&JSON:访问对象属性
- 错误:require.paths已删除.当运行node.js&socket.io
- Node.js&MongoDB:为什么数据库对象未定义