Disable logging in a node.js script running with forever
Asked Answered
S

3

11

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:

  1. Is it ok to keep forever running like this?
  2. 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.
  3. 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.

Staggers answered 10/6, 2013 at 21:5 Comment(3)
+1 for very nice question.Well
Have you considered implementing something like what most logging systems do? Instead of calling console.log directly, write a wrapper function(s) (or use a library) which allows you to assign not only a message, but a logging level, and then based on your configuration, only output logs which are of a certain significance or greater. I would say that's the "proper" way to disable logging.Carlina
@Bret I no longer work at that company, but that's what I ended up doing - creating various levels of logging. It kept the files small enough that the script rarely had to be stopped to clear the logs before restarting.Staggers
M
4

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.

Matteson answered 12/4, 2015 at 13:56 Comment(0)
H
2

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.

Hutchins answered 9/6, 2017 at 22:53 Comment(1)
I also use log4js but forever continues its own output.Morissa
B
1

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.

Barberabarberry answered 13/6, 2013 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.