Here’s the way of simple log rotation in Rails.
Environment
- Rails 4.1.8
- Ruby 2.2.2
- Ubuntu 15.04 (64bit)
Setting
First, add the following code to config/application.rb
.
1 2 3 4 |
# configure log rotation # split log by 100MB, limit log file count up to 10 config.logger = Logger.new( "#{Rails.root}/log/#{Rails.env}.log", 10, 104857600) |
In the above, log file will be split by 100 MB (104,857,600 bytes) and limit 10 files. Namely, it will save about 1 GB logs. また、 10 ファイル を超えると、古いものから削除されていきます。
Rails can configure logger as daily, weekly, etc., but in that case old log will not be deleted.
Test
Before testing in development environment, create log file with the following command.
1 |
dd if=/dev/zero of=log/development.log bs=1M count=100 |
bs
represents block size, count
represents times to write.
And launch server with rails s
, access some pages. Then, development.log
and development.log.0
will be created.
About Rails logging, you can use the other ways like SyslogLogger
and Linux logrotate, for example.