我还有一个问题 (last question)。目前我正在开发一个 Node.js 项目,其中我有 许多 console.log() 功能。到目前为止,这一切正常,但我也希望写入控制台的所有内容也写入日志文件。有人可以帮帮我吗?
例如:
Console.log('The value of array position [5] is '+ array[5]);
在我的真实代码中它有点多,但这应该会给你一个想法。
非常感谢。
最佳答案
只需像这样在终端中运行脚本...
node script-file.js > log-file.txt
这告诉 shell 将命令 node script-file.js
的标准输出写入您的日志文件,而不是默认的,将其打印到控制台。
这叫做 redirection它非常强大。假设您想将所有错误写入单独的文件...
node script-file.js >log-file.txt 2>error-file.txt
现在所有 console.log
都写入 log-file.txt
并且所有 console.error
都写入 error-文件.txt
关于javascript - txt 文件中的 Node.js console.log(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41232578/