Ruby on Rails 生产日志轮换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4883891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails production log rotation
提问by cnicolaou
What is the best way to enable log rotation on a Ruby on Rails production app?
在 Ruby on Rails 生产应用程序上启用日志轮换的最佳方法是什么?
Is it by using logrotate on the hosting server or is there a set of options to use when initialising logger from the app?
是通过在托管服务器上使用 logrotate 还是在从应用程序初始化记录器时使用一组选项?
回答by berkes
Option 1: syslog + logrotate
选项 1:系统日志 + logrotate
You can configure rails, to use the systems log tools.
您可以配置 rails,以使用系统日志工具。
An example in config/environments/production.rb.
config/environments/production.rb 中的示例。
# Use a different logger for distributed setups
config.logger = SyslogLogger.new
That way, you log to syslog, and can use default logrotate tools to rotate the logs.
这样,您就可以登录到 syslog,并且可以使用默认的 logrotate 工具来轮换日志。
Option 2: normal Rails logs + logrotate
选项 2:普通 Rails 日志 + 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.
另一种选择是简单地配置 logrotate 以获取 rails 留下的日志。例如,在 Ubuntu 和 Debian 上,这将位于名为/etc/logrotate.d/rails_example_com.
/path/to/rails.example.com/tmp/log/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
copytruncate
}
As per suggestions below, in Rails it is advised to use copytruncate, to avoid having to restart the Rails app.
根据以下建议,建议在 Rails 中使用copytruncate,以避免重新启动 Rails 应用程序。
Edit: removed "sharedscripts/endscript" since they are not used here and cause problems according to comment. And removed create 640 root admas per comment suggested.
编辑:删除了“sharedscripts/endscript”,因为它们在此处未使用并根据评论导致问题。并create 640 root adm根据评论建议删除。
回答by amit_saxena
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.
如果您正在使用 logrotate,那么您可以通过在 /etc/logrotate.d/ 目录中放置一个 conf 文件来选择下面显示的选项之一。
# Rotate Rails application logs based on file size
# Rotate log if file greater than 20 MB
/path/to/your/rails/applicaton/log/*.log {
size=20M
missingok
rotate 52
compress
delaycompress
notifempty
copytruncate
}
Or
或者
# Rotate Rails application logs weekly
/path/to/your/rails/applicaton/log/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
copytruncate
}
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 recommendthat 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.
请注意,copytruncate 会制作当前日志的备份副本,然后清除日志文件以继续写入。另一种方法是使用 create ,它将通过重命名当前文件然后创建一个与旧文件同名的新日志文件来执行轮换。我强烈建议您使用 copytruncate,除非您知道需要创建。原因是 Rails 可能仍然指向旧日志文件,即使其名称已更改,并且它们可能需要重新启动才能找到新日志文件。copytruncate 通过保持与活动文件相同的文件来避免这种情况。
回答by Fellow Stranger
For Rails 5, this is what I had to do to limit log size and don't change server output in the console:
对于 Rails 5,这是我必须做的以限制日志大小并且不更改控制台中的服务器输出:
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').
根据文档,如果您想限制日志文件夹的大小,请将其放在您的环境文件('development.rb'/'production.rb')中。
config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 50 * 1024 * 1024)
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.
有了这个,您的日志文件永远不会超过 50Mb。您可以根据自己的喜好更改大小。第二个参数中的“1”表示将保留 1 个历史日志文件,因此您将拥有多达 100Mb 的日志——当前日志和前一个 50Mb 的块。
回答by Rael Gugelmin Cunha
For Rails 5, if you want daily log rotation, you only need this:
对于Rails 5,如果你想要每日日志轮换,你只需要这个:
config.logger = ActiveSupport::Logger.new(config.paths['log'].first, shift_age = 'daily')
According the documentation, you can use daily, weeklyor monthly.
根据文档,您可以使用daily,weekly或monthly。
回答by ThienSuBS
For every log: Rails log, Rpush log, ... You can use like this in your config file of service:
对于每个日志:Rails 日志、Rpush 日志、...您可以在服务的配置文件中这样使用:
config.log_file = 'log/rpush.log'
config.logger = ActiveSupport::Logger.new(config.log_file, 1, 20*1024*1024)
It means: only save 1 previous log file after split. Main log size never over 20 MB.
这意味着:拆分后只保存 1 个以前的日志文件。主日志大小从不超过 20 MB。
回答by riya khana
Enable to send logs to the loggly using rails logglier as following in my environments/production.rb file. rails version is 4.1.0
启用使用 rails logglier 将日志发送到 loggly,如下所示在我的环境/生产.rb 文件中。rails 版本是 4.1.0
RailsApplication::Application.configure do
require 'logglier'
config.logger = Logglier.new(<https://logs-01.loggly.com/inputs/inputkey>)
log.info("hello from logglier")
end

