Ruby on rails log file size too large

2019-03-09 17:28发布

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

8条回答
贼婆χ
2楼-- · 2019-03-09 18:06

You may want to use logrotate. Have a look at the answer to this question: Ruby on Rails production log rotation.

查看更多
姐就是有狂的资本
3楼-- · 2019-03-09 18:09

According to the documentation, if you want to limit the size of the log folder, put this in your 'development.rb'-file:

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.

查看更多
我命由我不由天
4楼-- · 2019-03-09 18:14

The log folder 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 task is provided to allow the easy clearing of the log files.

rake log:clear
# Truncates all *.log files in log/ to zero bytes 
# Specify which logs with LOGS=test,development,production
查看更多
Anthone
5楼-- · 2019-03-09 18:14

A fair compromise, in an initializer:

Rake::Task['log:clear'].invoke if Rails.env.development? || Rails.env.test?
查看更多
叛逆
6楼-- · 2019-03-09 18:22

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

查看更多
甜甜的少女心
7楼-- · 2019-03-09 18:22

config.logger = ActiveSupport::Logger.new(nil) does the trick and completely disables logging to a file (console output is preserved).

查看更多
登录 后发表回答