How to add console outputs Node.js app in the log access NGINX file?

安稳与你 提交于 2020-01-24 09:48:06

问题


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 ?

Thanks in advance.


回答1:


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);

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

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




回答2:


Brief :

Using JFile package , file logging can be smooth as following :

nxFile.text+='\n'+message;

Details :

Add function that logs on both (Terminal+nginx log) , then use it instead of using console.log directly :

var customLog=function(message){

     console.log(message);
     logNginx(message);
}

Then , implement logNginx which is called inside customLog :

    var JFile=require('jfile'); //  "npm install jfile --save" required
    let nxFile=new JFile('/var/log/nginx/access.log'); // check path before if exist in your system . IF no , change it with the available path 
    function logNginx(message){
        nxFile.text+='\n'+message; //append new line in nginx log file
      }

Don't forget to install JFile npm install jfile which makes handling files done quickly .




回答3:


You can add the following code in your nginx script . This should work

env NODE_BIN=/usr/bin/node
env SCRIPT_FILE="server.js"
env LOG_FILE=/var/log/logfilename.log
env RUN_AS="root"


$RUN_AS -- $NODE_BIN $SCRIPT_FILE >> $LOG_FILE 2>&1



回答4:


If you're running Node as a systemd process, with console.log going to stdout (which I believe is the default), and your goal is just to see the logs (or get them on disk somewhere), there's an easier way than all this Node meddling and hooking.

You should already have access to the console log without doing anything through journalctl. For instance, my systemd unit file (at /etc/systemd/system/myapp.service in this example) looks something like this:

[Unit]
Description=My Webapp

[Service]
WorkingDirectory=/srv/webapp-dir
ExecStart=/usr/local/bin/node server.js
Restart=always
RestartSec=5
Environment=NODE_ENV=production PORT=1984
User=myuser
Group=myuser

[Install]
WantedBy=multi-user.target

And running journalctl -u myapp shows me the console logs from my app.

If you want, you can also send those logs to the syslog with some additional parameters. I've also added the following to my [Service] directory to do so:

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myapp

Which results in my logs going to the syslog tagged with myapp, where I could filter them into their own log if I wanted to with rsyslog filtering.



来源:https://stackoverflow.com/questions/38551624/how-to-add-console-outputs-node-js-app-in-the-log-access-nginx-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!