我正在研究 Nodejs 中的各种包,但找不到适合我需要的包。 我想输出当前行号、函数名称和要记录的 src 文件,类似于 Bunyan 记录器的做法。
我研究了“caller-id”等软件包,但我无法使用它,因为它不支持“严格”模式。
其他包,例如“capture-stack-trace”,仅返回文件名,而不返回行号和函数名。
是否有我缺少的软件包可以满足我的需要? 或者是否有一种优雅的方式在代码中生成此信息?
提前致谢。
最佳答案
默认情况下,在每个 Node 记录器中始终关闭确定调用源信息。它很慢。切勿在生产中使用此选项。
我们使用bunyan为我们的微服务。这是从 documentation 复制粘贴的:
The source file, line and function of the log call site can be added to log records by using the src: true config option:
var log = bunyan.createLogger({src: true, ...});
This adds the call source info with the 'src' field, like this:
{ "name": "src-example", "hostname": "banana.local", "pid": 123, "component": "wuzzle", "level": 4, "msg": "This wuzzle is woosey.", "time": "2012-02-06T04:19:35.605Z", "src": { "file": "/Users/trentm/tm/node-bunyan/examples/src.js", "line": 20, "func": "Wuzzle.woos" }, "v": 0 }
关于node.js - Nodejs - 打印函数名称、行号和文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46630800/