Ruby on Rails 生产日志轮换
在 Ruby on Rails 生产应用程序上启用日志轮转的最佳方法是什么?
是通过在托管服务器上使用 logrotate 还是在从应用程序初始化记录器时使用一组选项?
What is the best way to enable log rotation on a Ruby on Rails production app?
Is it by using logrotate on the hosting server or is there a set of options to use when initializing logger from the app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
选项1:syslog + logrotate
您可以配置rails,以使用系统日志工具。
config/environments/Production.rb 中的示例。
这样,您就可以记录到 syslog,并且可以使用默认的 logrotate 工具来轮换日志。
选项2:普通Rails日志+logrotate
另一种选择是简单地配置logrotate来拾取rails留下的日志。
例如,在 Ubuntu 和 Debian 上,该文件位于名为
/etc/logrotate.d/rails_example_com
的文件中。根据下面的建议,在 Rails 中建议使用
copytruncate
,以避免重新启动 Rails 应用程序。编辑:删除了“sharedscripts/endscript”,因为它们在这里没有使用,并且根据评论会导致问题。并按照建议的评论删除了
create 640 root adm
。Option 1: syslog + logrotate
You can configure rails, to use the systems log tools.
An example in config/environments/production.rb.
That way, you log to syslog, and can use default logrotate tools to rotate the logs.
Option 2: normal Rails logs + logrotate
Another option is to simply configure logrotate to pick up the logs left by rails.
On Ubuntu and Debian that would be, for example, in a file called
/etc/logrotate.d/rails_example_com
.As per suggestions below, in Rails it is advised to use
copytruncate
, to avoid having to restart the Rails app.Edit: removed "sharedscripts/endscript" since they are not used here and cause problems according to comment. And removed
create 640 root adm
as per comment suggested.如果您使用 logrotate,那么您可以通过将 conf 文件放置在 /etc/logrotate.d/ 目录中来选择下面显示的任一选项。
或者
请注意,copytruncate 会生成当前日志的备份副本,然后清除日志文件以便继续写入。另一种方法是使用 create,它将通过重命名当前文件然后创建与旧文件同名的新日志文件来执行轮换。 我强烈建议您使用copytruncate,除非您知道需要create。原因是 Rails 可能仍会继续指向旧的日志文件,即使其名称已更改,并且可能需要重新启动才能找到新的日志文件。 copytruncate 通过保留与活动文件相同的文件来避免这种情况。
If you are using logrotate then you can choose either of the options shown below by placing a conf file in the /etc/logrotate.d/ directory.
Or
Please note that copytruncate makes a backup copy of the current log and then clears the log file for continued writing. The alternative is to use create which will perform the rotation by renaming the current file and then creating a new log file with the same name as the old file. I strongly recommend that you use copytruncate unless you know that you need create. The reason why is that Rails may still keep pointing to the old log file even though its name has changed and they may require restarting to locate the new log file. copytruncate avoids this by keeping the same file as the active file.
对于 Rails 5,我必须这样做来限制日志大小并且不要更改控制台中的服务器输出:
根据文档,如果您想限制日志文件夹的大小,请将其放入您的环境文件('development.rb '/'生产.rb')。
这样,您的日志文件将永远不会增长到超过 50Mb。您可以根据自己的喜好更改尺寸。第二个参数中的“1”表示将保留 1 个历史日志文件,因此您将拥有最多 100Mb 的日志 – 当前日志和前一个 50Mb 的日志块。
此解决方案的来源。
For Rails 5, this is what I had to do to limit log size and don't change server output in the console:
According to the documentation, if you want to limit the size of the log folder, put this in your environment-file ('development.rb'/'production.rb').
With this, your log files will never grow bigger than 50Mb. You can change the size to your own preference. The ‘1’ in the second parameter means that 1 historic log file will be kept, so you’ll have up to 100Mb of logs – the current log and the previous chunk of 50Mb.
Source to this solution.
对于Rails 5,如果您想要每日日志轮换,您只需要这样做:
根据文档,您可以使用
daily
、weekly
或monthly< /代码>。
For Rails 5, if you want daily log rotation, you only need this:
According the documentation, you can use
daily
,weekly
ormonthly
.对于每个日志:Rails 日志、Rpush 日志...
您可以在服务的配置文件中这样使用:
这意味着:分割后仅保存 1 个先前的日志文件。
主日志大小永远不会超过 20 MB。
For every log: Rails log, Rpush log, ...
You can use like this in your config file of service:
It means: only save 1 previous log file after split.
Main log size never over 20 MB.
启用使用rails logglier将日志发送到loggly,如下我的environments/development.rb文件中所示。
Rails 版本是 4.1.0
Enable to send logs to the loggly using rails logglier as following in my environments/production.rb file.
rails version is 4.1.0