How to get logs of NodeJs application running in forever npm? - node.js

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

Related

Ubuntu where to see console.logs

So I am putting my app on a server and I want to see some console logs generated from my nodejs app. Before putting it on the server I saw the output of my console log on my terminal, but where do I find the output for these on Ubuntu? I'm running version 16.04 on Ubuntu
Lifted straight from #SlovyanskiyYehor comment two years ago, but I missed his answer as a comment. Also answered here: Make pm2 log to console.
If you are using pm2 to start your node.js application, you can use:
pm2 logs
or for just a single app running in pm2:
pm2 logs yourAppNameHere
If you are using some bash script which is runed by cron for example,
you can specify file where you need to output all logs of your app
It will looks like that:
myApp > /path/to/logs/myAppLogs.txt
This command will run application myApp and put all logs into
"/path/to/logs/myAppLogs.txt"
Also you can put this string into cron directly without some bash scripts
Of course you can see all console logs by user here ~/.bash_history
but it`s not always useful
If you used pm2, you can check log in this folder:
/root/.pm2/log

StrongLoop's LoopBack.io: How to turn on HTTP access log?

I searched the docs, but found no answer. What is the preferred way of turning on access logging?
I expect HTTP verb, requested path, source IP address,... printed to either standard output or a log file.
All I got so far is:
Browse your REST API at http://0.0.0.0:3000/explorer
Web server listening at: http://0.0.0.0:3000/
The server responds to requests but I cannot see any logs.
I currently need to run the app using slc run (no process manager).
You can achieve the logs in a separate file by just passing the log file name.
for example:-
slc run -d -l /tmp/file.log -p /tmp/file.pid -d
-d will detach the process from your current screen, and will run it in the background
Go through the below links, for further clarifications:-
http://docs.strongloop.com/display/NODE/slc+run
http://docs.strongloop.com/display/SLC/Logging
And for choosing the correct logger:-
http://docs.strongloop.com/display/SLC/Using+logging+libraries
Winston and bunyan are 2 well suited loggers, we are using bunyan and it is working really good for us.
If you want to RUN the slc in the DEBUG mode, to check all the logs, you can run using
DEBUG=* slc run
Since loopback is based on express, you could start with something like morgan:
var morgan = require('morgan');
app.use(morgan('combined'))

How to get forever to print child process output to stdout.

I'm running node app under forever in a docker container, how do I get all console.log output to show up on the console? I'm a beginner to docker so I assume the best way to log with docker is to simply log to stdout. If there's a better more proper way to do this I'm open to alternative solutions.
forever, like standard node, simply logs to stdout as well so you shouldn't have to do anything special. Of course, if you start your container with docker run -d you'll have to run docker logs -f myNodeContainer to actually see the output live.
I just came across this question because I also installed forever for the first time, ran it with just forever server.js and...nothing.
No node console messages, just the warnings about no --minUptime and no --spinSleepTime. I stopped the forever processes using forever stopall and went looking for the logs. Sure enough, in the forever log was all the stdout from my server.
I assumed I was missing an option to print to stdout, but apparently not. After reading this SO question, I launch a fresh terminal, run it again and it works - it now logs my server messages to the console.
Since then I have also experienced lots of strange issues with forever. Like it restarting the script continuously, even though the script fails to start within 1000ms (there was a syntax error in one of the files) - I stop it, run forever again (same options, same source files) and it does what it's supposed to (only runs the script once).
Not really an answer (sorry), but this is just in case anyone comes seeking information.

Disable logging in a node.js script running with forever

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.

automatically restarting service via forever for nodejs [duplicate]

This question already has answers here:
Restart node upon changing a file
(9 answers)
Closed 7 years ago.
I found that forever can run nodejs server forever. Is forever supports this feautre?
-- If the nodejs script is modified changed, the server shld restarted automatically.
How can I enable this feature using forever? or I need something else?
From the forever readme. Use the -w flag to watch file for changes.
In case someone else, like myself, comes to find this through google.
I have to run it thusly:
forever --watch ./start/file
For me, at least, it defaults to watching the current directory I'm running the command in for changes. ./start/file is the file that "npm start" hits up from your package.json.
If you need to watch a different directory from where you're pwd shows you to be, try:
forever --watch --watchDirectory ./path/to/dir ./start/file
For some reason "forever start xxxxxxxxx" only brings up the help information for me, but this works. /me shrugs.
Again just another example of its usage (and it does work :D)
forever -w --watchDirectory . --watchIgnore *.log -o ./log/out.log -e ./log/err.log index.js
That will launch the app in the same process with output to stdout/stderr (but also written to the logs)
To launch it in prod watching is obviously not a good idea and running it as a deamon is probably what you are after so drop the -w flags and add the "start" command
forever -o ./log/out.log -e ./log/err.log start index.js
I personally use Nodemon to handle that. Its a replacement for the node server. It automatically restarts the server when your files are updated. You might want to check it out.

Resources