gpt4 book ai didi

node.js - 如何在日志访问 NGINX 文件中添加控制台输出 Node.js 应用程序?

转载 作者:搜寻专家 更新时间:2023-10-31 23:37:32 26 4
gpt4 key购买 nike

我有一个使用 systemd 设置的 Node.js 应用程序。运行在 NGINX 后面的应用。
我想在日志访问 NGINX 文件中添加我的 Node.js 应用程序的控制台输出?
我该怎么做?

提前致谢。

最佳答案

更简单的方法是 Hook console.log 并照常调用 console.log

var util = require('util');
var JFile = require("jfile");
var nxFile = new JFile('/var/log/nginx/access.log');
...
process.stdout.write = (function(write) {
return function(text, encoding, fd) {
write.apply(process.stdout, arguments); // write to console
nxFile.text += util.format.apply(process.stdout, arguments) + '\n'; // write to nginx
}
})(process.stdout.write);

您还可以通过将上面代码中的 stdout 更改为 strerr 来定义对 console.error 的 Hook 。

附言我没有 nginx 来验证代码。所以代码可能包含错误:)

关于node.js - 如何在日志访问 NGINX 文件中添加控制台输出 Node.js 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com