相关疑难解决方法(0)

获取node.js中的调用函数的名称和行

如何获得调用当前函数的函数的名称和行?我希望有一个像这样的基本调试函数(使用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)

是否有一个节点等效于完成此操作?

javascript node.js

51
推荐指数
5
解决办法
5万
查看次数

将摩根与记录器一起使用时,stream.write 不是函数

基本上我正在尝试使用摩根和温斯顿为 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)

logging node.js winston morgan

4
推荐指数
2
解决办法
4668
查看次数

标签 统计

node.js ×2

javascript ×1

logging ×1

morgan ×1

winston ×1