I need to debug the node-apn (some pushes are getting lost), so i need to analyze node-apn logs. Therefore, i want to store node-apn logs in a file.
What i have tried
I have enabled the node-apn logs and they are appearing on my console. Now, i am running following commands to start the server but i could not see node-apn logs in the file. I can see application logs (generated by winston) in file.
sudo node app.js test >> /home/gaurav/temp.txt
sudo node app.js test | tee /home/gaurav/temp.txt
Can anybody suggest how to achieve this.
The logs are probably being sent to stderr instead of stdout, so try this:
sudo node app.js test >> /home/gaurav/temp.txt 2>&1
sudo node app.js test 2>&1 | tee /home/gaurav/temp.txt
The 2>&1 redirects everything being sent to stderr to stdout.
Related
I am trying to configure my php errors to output to docker logs. All documentation I have read indicates that docker logs are tied to the containers stdout and stderr which come from /proc/self/fd/1 and /proc/self/fd/2. I created a symlink from my php error log file /var/log/php_errors.log to /proc/self/fd/1 with command:
ln -sf /proc/self/fd/1 /var/log/php_errors.log
After linking the error log I have tested its functionality by running this php script:
<?php
error_log("This is a custom error message constructed to test the php error logging functionality of this site.\n");
?>
The output echos the error message to the console so I can see that php error logging is now redirected to stdout in the container, but when I run docker logs -f <containername> I never see the error message in the logs. Also echoing from inside the container doesn't show in the logs either which is confusing because my understanding is the echo command is stdout.
Further reading informed me that docker logs will only show output from pid 1 which could be the issue. If this is the case how can I correctly configure my php error logging to show in docker logs outside the container.
Also I have checked that I am using the default json-file docker driver, and have tried this both on my local environment and a web server.
I was wondering, is there any way I can check out the logs of the my nodejs server when I've forever'd it using forever start server.js to look at what's being logged, check any live errors and such.
I looked at their documentation but couldn't find anything related to this. I want to be able to look at the console.
As forever start an application on background it gets hard to access old logs.
If you can use a library for maintaining logs, I will suggest simple-node-logger . Using this library you can save all your logs on a file and can access that file any time using:
tail -f logsFile.txt
Using this command this will keep printing new changes over logsFile.txt as logger keep updating logs.
This will help you to maintain all the logs for you.
First find out where your logs are stored using:
sudo forever list
for each process you can see something like this as logfile:
/root/.forever/_Jht.log
use 'tail -f' to monitor its changes:
sudo tail -f /root/.forever/_Jht.log
You can also set your desired path for log files, see this:
https://stackoverflow.com/a/21021251/1119611
I start a nodejs app using pm2, the log out-file config setting is default.
Now, i find the log is too huge, so i need to redirect the log to /dev/null without restarting the process and without using pm2 -logrotate.
Is there any way around this issue?
You will have to restart the process if you want a fresh redirection. If restarting is okay, then it could be like:
pm2 'command' > /dev/null
If not, then you can possibly write a very basic shell script and schedule it via cron. It would just clean your logs (pm2 flush), or compress and dump (whichever you prefer).
I'm running a node app via a shell script
#STARTUP.sh
node server.js
#/etc/rc.local - this runs on startup of my machine
sudo sh startup.sh >> /home/pi/logs/app.log 2>&1
My issue is, when I have an error in my node app it just crashes and doesn't write the error to app.log
Is there a way to make sure when the app crashes that the error gets logged as well?
Replace the last line with this:
sudo sh startup.sh &>>/home/pi/logs/app.log
I am continually running a few server scripts (on different ports) with nodejs using forever.
There is a considerable amount of traffic on some of these servers. The console.log commands I have for tracking connection anomalies result in bloated log files that I don't need all of the time - only for debugging. I have been manually stopping the scripts late at night, truncating the files, and restarting them. This won't do for long term, so we decided to find a solution.
Someone else on my system deleted the log files I had set up for each of the servers without my knowledge. Calling forever list on the command line shows that the server scripts are still running but now I can't tail the log files to see how the nodes are doing.
Node downtime should be kept to a bare minimum, so I'm hesitant to stop the servers during daylight hours for longer than a few minutes. Initial testing from the client side seems to indicate that the scripts are doing fine, but I can't be 100% sure there are no errors due to failed attempts at logging to a nonexistent file.
I have a few questions actually:
Is it ok to keep forever running like this?
If not, is there a proper way to disable logging? The github repository seems to indicate that forever will still log to a default file, which I don't want. Otherwise I may just write a cronjob that periodically stops scripts, truncates logs, then restarts the scripts.
What happens if I just create the logfile again with something like touch logfile_name.log while the script is still running - will this make forever freak out or is this a plausible solution?
Thanks for your time.
according to https://github.com/foreverjs/forever, try to pass -s to silent all log.
forever -s start YOURSCRIPT
Surely, before doing this, try to update forever to the latest:
sudo curl -L https://npmjs.com/install.sh | sudo sh
sudo npm update -g.
1) Just build in a periodic function or admin option to clear the forever logs. From the manual forever cleanlogs
2) At least for linux. Send each log file to /dev/null. Each log type is specified by options -l -o and -r. The -a option for append log, will stop it complaining about the log already existing.
forever start -a -l /dev/null -o /dev/null -r /dev/null your-server.js
Perhaps employ your own logging system, I use log4js, it doesn't complain if I delete the log file while the node process is still running.
There's a nifty tool that can help you that called logrotate. Have a look here
Especially the copytruncate option, it is very useful in your case.