Ruby on Rails 生产日志轮换

发布于 2024-10-16 01:57:30 字数 98 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苏璃陌 2024-10-23 01:57:30

选项1:syslog + logrotate

您可以配置rails,以使用系统日志工具。

config/environments/Production.rb 中的示例。

# Use a different logger for distributed setups
config.logger = SyslogLogger.new

这样,您就可以记录到 syslog,并且可以使用默认的 logrotate 工具来轮换日志。

选项2:普通Rails日志+logrotate

另一种选择是简单地配置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
}

根据下面的建议,在 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.

# 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.

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.

/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.

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.

黑色毁心梦 2024-10-23 01:57:30

如果您使用 logrotate,那么您可以通过将 conf 文件放置在 /etc/logrotate.d/ 目录中来选择下面显示的任一选项。

# 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
}

或者

# Rotate Rails application logs weekly
/path/to/your/rails/applicaton/log/*.log {
  weekly
  missingok
  rotate 52
  compress
  delaycompress
  notifempty
  copytruncate
}

请注意,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.

# 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 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.

耳钉梦 2024-10-23 01:57:30

对于 Rails 5,我必须这样做来限制日志大小并且不要更改控制台中的服务器输出:

根据文档,如果您想限制日志文件夹的大小,请将其放入您的环境文件('development.rb '/'生产.rb')。

config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 50 * 1024 * 1024)

这样,您的日志文件将永远不会增长到超过 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').

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.

Source to this solution.

一腔孤↑勇 2024-10-23 01:57:30

对于Rails 5,如果您想要每日日志轮换,您只需要这样做:

  config.logger = ActiveSupport::Logger.new(config.paths['log'].first, shift_age = 'daily')

根据文档,您可以使用 dailyweeklymonthly< /代码>。

For Rails 5, if you want daily log rotation, you only need this:

  config.logger = ActiveSupport::Logger.new(config.paths['log'].first, shift_age = 'daily')

According the documentation, you can use daily, weekly or monthly.

一身骄傲 2024-10-23 01:57:30

对于每个日志:Rails 日志、Rpush 日志...
您可以在服务的配置文件中这样使用:

 config.log_file = 'log/rpush.log'
 config.logger = ActiveSupport::Logger.new(config.log_file, 1, 20.megabytes)

这意味着:分割后仅保存 1 个先前的日志文件。
主日志大小永远不会超过 20 MB。

For every log: Rails log, Rpush log, ...
You can use like this in your config file of service:

 config.log_file = 'log/rpush.log'
 config.logger = ActiveSupport::Logger.new(config.log_file, 1, 20.megabytes)

It means: only save 1 previous log file after split.
Main log size never over 20 MB.

人生百味 2024-10-23 01:57:30

启用使用rails logglier将日志发送到loggly,如下我的environments/development.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

Enable to send logs to the loggly using rails logglier as following in my environments/production.rb file.
rails version is 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文