将节点调试日志的内容写入文件

Write the contents of node debug log to file

我可以将这个输出的内容写入文件吗?我正在使用 debug 模块来记录消息,我希望能够将它们通过管道传输到文件中。但是它没有按预期工作。

$ DEBUG=* node -e 'var debug = require("debug")("test"); debug("hello world")'
    test hello world +0ms
$ DEBUG=* node -e 'var debug = require("debug")("test"); debug("hello world")' > temp.txt
    test hello world +0ms

刚刚试过了,也没有收到任何输出。

$ { DEBUG=* node -e "var debug = require('debug')('test'); debug('hello world')"; } >temp.txt
    test hello world +0ms
  1. 确保 运行 是 debug 的最新版本。
  2. 您需要使用 DEBUG_FD=3 env 标志以及 3> 来管道

这是一个例子。

$ DEBUG_FD=3 DEBUG=foo node -e "require('debug')('foo')('hello')" 3> foo.txt

来自 repo 的问题 request/question: is there a way for debug to log to a file?

使用 DEBUG_FD=3 对我不起作用。

对我有用的选项是使用 2> 到管道:

DEBUG=* node --inspect=0.0.0.0:9229 . 2> log.txt