23941

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

Question:

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

Thanks in advance.

Answer1:

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

Answer2:

Brief :

Using <a href="https://github.com/abdennour/jfile" rel="nofollow">JFile</a> 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 <strong>logNginx</strong> which is called inside <strong>customLog</strong> :

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 } <hr />

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

Answer3:

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

Recommend

  • Using Runtime.getRuntime().exec() to run perl code in java
  • gunicorn and django project (no app)
  • thin: waiting for n connection to close while trying to stop server
  • Getting part of nginx url as “variable”?
  • Nginx: Permission denied to Gunicorn socket on CentOS 7
  • How can I edit the NGINX configuration on Google App Engine flexible environment?
  • How to reverse proxy a site which use ssl by nginx?
  • Nginx serving server and static build
  • How do I run two rails apps behind the same domain and have nginx route requests based on cookie?
  • Does iOS support anything outside of HTTP Live Streaming?
  • How to deploy AngularJS app using nginx + docker to Elastic Beanstalk?
  • How to enable SSL/HTTPS on bokeh 0.12.5?
  • too many redirects for wordpress on nginx with apache2
  • Stale-while-revalidate cache replacement from Varnish
  • Deployment Rails app for Docker
  • How to exit “docker run” containers once the script those containers execute calls exit()
  • How write a unit test for verifying compiling error?
  • Can't configure nginx as a proxy for tomcat with context path
  • Read stdin in chunks in Bash pipe
  • triggering user space with kernel
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • MySQL WHERE-condition in procedure ignored
  • How to handle AllServersUnavailable Exception
  • Web-crawler for facebook in python
  • trying to dynamically update Highchart column chart but series undefined
  • coudnt use logback because of log4j
  • How do I configure my settings file to work with unit tests?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?