如何获得调用当前函数的函数的名称和行?我希望有一个像这样的基本调试函数(使用npmlog定义log.debug
):
function debug() {
var callee, line;
/* MAGIC */
log.debug(callee + ":" + line, arguments)
}
Run Code Online (Sandbox Code Playgroud)
当从另一个函数调用时,它将是这样的:
function hello() {
debug("world!")
}
// outputs something like:
// "hello:2 'world!'"
Run Code Online (Sandbox Code Playgroud)
为清楚起见,我想要的基本上类似于Python:
import inspect
def caller():
return inspect.stack()[2][3]
// line no from getframeinfo().lineno
Run Code Online (Sandbox Code Playgroud)
是否有一个节点等效于完成此操作?
基本上我正在尝试使用摩根和温斯顿为 nodejs 实现记录器。
当我尝试使用 morgan 时,抛出 stream.write 错误不是函数。
因为我想获取文件名,所以我正在传递模块,从模块对象中有一个名为 filename 的属性。
下面是我的代码。
// 温斯顿.js
const appRoot = require('app-root-path');
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;
const path = require('path');
// Custom Format
const customFormat = printf(info => {
return `${new Date(info.timestamp)} || [${info.label}] || ${info.level}: ${info.message} `
})
// Return the last folder name in the path and the calling
// module's filename.
const getLabel = function (moduleDetails) {
if …
Run Code Online (Sandbox Code Playgroud)