我有一个使用systemd设置的Node.js应用程序.应用程序在NGINX后面运行.
我想在日志访问NGINX文件中添加我的Node.js应用程序的控制台输出?
我怎样才能做到这一点 ?
提前致谢.
解决方法:
更简单的方法是钩子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的挂钩.
附:我没有nginx来验证代码.所以代码可能包含错误:)
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!