javascript - 使用 debug 在 Node.js 中打印函数名称和参数

标签 javascript node.js

我想使用debug能够在每次调用时打印方法/函数名称和参数。

实现此目的的最佳方法是什么,而不是像这样在每个函数中放置自定义的 debug 语句:

async function getFilePaths(path, id) {
  debug(`async function getFilePaths(path = ${path}, id = ${id})`);
  // ...
}

最佳答案

你可以这样做:

function logDebug(fn, args) {
   debug(fn.name + args.toString());
}

并在你的函数中调用它

function getFilePaths(path, id) {
  logDebug(getFilePaths, arguments);
}

注意:Function.name是ES6中支持的一项新技术。

关于javascript - 使用 debug 在 Node.js 中打印函数名称和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31445852/

相关文章:

javascript - 修改输入 float 标签脚本以也适用于文本区域

javascript - 尝试从数组中添加和删除项目

javascript - 如何使用 JQUERY 删除禁用元素?

javascript - 有人可以帮我在 Node.Js 中设置缓冲区吗?

node.js - 无法将 firebase 函数迁移到 Node 10 运行时

javascript - 找不到 sharejs 对象的方法 'attach'

node.js - Teamcity 运行的 Grunt 无法正确输出到日志

javascript - Mapbox GL JS 中的 GeoJson 不会通过 URL 接受它,格式看起来不错

javascript - 为什么我的express.js 无法运行?

javascript - 检查 TypeScript 中 sinon stub 的参数