Ruby on rails 日志文件太大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7784057/
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 log file size too large
提问by Jason
I stumbled to learn that my rails3.1 log file is super large, around 21mb. Is this, in terms of size normal? What the log file would like in the production environment? Besides, can I get rid of the log?thanks
我偶然发现我的 rails3.1 日志文件非常大,大约 21mb。就尺寸而言,这是正常的吗?生产环境中的日志文件是什么样的?此外,我可以摆脱日志吗?谢谢
采纳答案by Michael Durrant
you can just delete the file!
Rails will create a new log if one doesn't exist.
Obviously save / backup the file if it's important, but usually it's not.
You can also zip the backuped up file (and then delete the source) if you want to keep it on the same drive but still save space.
你可以删除文件!
如果不存在,Rails 将创建一个新日志。
如果文件很重要,显然保存/备份文件,但通常不是。
如果您想将备份文件保存在同一个驱动器上但仍然节省空间,您还可以压缩备份文件(然后删除源文件)。
To automatically rotate log files (the best long-term solution) use log rotate as described here:
要自动轮换日志文件(最好的长期解决方案),请按此处所述使用日志轮换:
Ruby on Rails production log rotation
then you can set it and forget it!
然后你可以设置它并忘记它!
To actually change what gets logged see:
要实际更改记录的内容,请参阅:
http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/
http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/
回答by Raj Adroit
The logfolder of your Rails application holds three log files corresponding to each of the standard environments. Log files can grow very large over time. A rake taskis provided to allow the easy clearing of the log files.
logRails 应用程序的文件夹包含与每个标准环境对应的三个日志文件。随着时间的推移,日志文件会变得非常大。rake task提供A以允许轻松清除日志文件。
rake log:clear
# Truncates all *.log files in log/ to zero bytes
# Specify which logs with LOGS=test,development,production
回答by Fellow Stranger
According to the documentation, if you want to limit the size of the log folder, put this in your 'development.rb'-file:
根据文档,如果您想限制日志文件夹的大小,请将其放在您的“development.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 htanata
You may want to use logrotate. Have a look at the answer to this question: Ruby on Rails production log rotation.
您可能想要使用logrotate. 看看这个问题的答案:Ruby on Rails production log rotation。
回答by Drew Stephens
I automatically clear the logs in development on each server start with config/initializers/clear_development_log.rb:
我会自动清除每台服务器上的开发日志,开头为config/initializers/clear_development_log.rb:
if Rails.env.development?
`rake log:clear`
end
回答by ThienSuBS
Yes, You can using syntax like this:
是的,您可以使用这样的语法:
config.logger = ActiveSupport::Logger.new(config.log_file, num_of_file_to_keep, num_of_MB*1024*1024)
Example:
例子:
config.logger = ActiveSupport::Logger.new(config.log_file, 2, 20*1024*1024)
It not only using for Rails log, you can using log file of any services run with rails, such as: rpush log, ...
它不仅用于 Rails 日志,您还可以使用 Rails 运行的任何服务的日志文件,例如:rpush log, ...
回答by Artur Beljajev
config.logger = ActiveSupport::Logger.new(nil)does the trick and completely disables logging to a file (console output is preserved).
config.logger = ActiveSupport::Logger.new(nil)这样做并完全禁用记录到文件(控制台输出被保留)。
回答by Dorian
A fair compromise, in an initializer:
一个公平的妥协,在初始化程序中:
Rake::Task['log:clear'].invoke if Rails.env.development? || Rails.env.test?

