简体   繁体   English

禁用永久运行的node.js脚本中的日志记录

[英]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. 我不断使用nodejs运行一些服务器脚本(在不同的端口上)。

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. 我用于跟踪连接异常的console.log命令导致我不需要一直都需要的膨胀日志文件 - 仅用于调试。 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 . 客户端的初始测试似乎表明脚本运行正常,但我不能100%确定没有因为尝试登录到不存在的文件失败而导致错误。

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. github存储库似乎表明永远仍然会记录到我不想要的默认文件。 Otherwise I may just write a cronjob that periodically stops scripts, truncates logs, then restarts the scripts. 否则我可能只是写一个cronjob定期停止脚本,截断日志,然后重新启动脚本。
  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? 如果我在脚本仍在运行时再次使用touch logfile_name.log之类的东西再次创建日志文件会发生什么 - 这会让这一点永远变得怪异或者这是一个看似合理的解决方案吗?

Thanks for your time. 谢谢你的时间。

according to https://github.com/foreverjs/forever , try to pass -s to silent all log. 根据https://github.com/foreverjs/forever ,尝试将-s传递给silent所有日志。

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 . sudo npm update -g

There's a nifty tool that can help you that called logrotate. 有一个漂亮的工具可以帮助你调用logrotate。 Have a look here 看看这里

Especially the copytruncate option, it is very useful in your case. 特别是copytruncate选项,它在您的情况下非常有用。

1) Just build in a periodic function or admin option to clear the forever logs. 1)只需构建一个定期函数或管理选项来清除永久日志。 From the manual forever cleanlogs 从手册forever cleanlogs

2) At least for linux. 2)至少对于linux。 Send each log file to /dev/null . 将每个日志文件发送到/dev/null Each log type is specified by options -l -o and -r . 每个日志类型由选项-l -o-r指定。 The -a option for append log , will stop it complaining about the log already existing. append log-a选项将阻止它抱怨已存在的日志。

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. 也许使用你自己的日志系统,我使用log4js ,如果在节点进程仍在运行时删除日志文件,它不会抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM