如何在日志访问NGINX文件中添加控制台输出Node.js应用程序? [英] How to add console outputs Node.js app in the log access NGINX file?

查看:265
本文介绍了如何在日志访问NGINX文件中添加控制台输出Node.js应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Node.js app setting up with systemd. The app running behind NGINX.
I would like to add console output of my Node.js application in the log access NGINX file ?
How can I do this ?

先谢谢了.

推荐答案

更简单的方法是挂接console.log并像往常一样调用console.log.

More simple way is hook console.log and call console.log as usually.

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的钩子.

Also you can define hook to console.error by change stdout to strerr in code above.

P.S.我没有Nginx来验证代码.所以代码可能包含错误:)

P.S. I don't have nginx to verify code. So code can contains errors :)

这篇关于如何在日志访问NGINX文件中添加控制台输出Node.js应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆