Rails.logger
suggest changeUse Rails.logger.debug, info, warn, error, or fatal rather than puts. The log line then has the standard format, a timestamp, and a level, so you can decide what a given environment shows. Files live in log/ and are named after RAILS_ENV, such as development.log and production.log. staging.log appears only when you boot with RAILS_ENV=staging.
Rotate production logs with logrotate:
Open /etc/logrotate.conf with your favourite linux editor vim or nano and add the below code in this file at bottom.
/YOUR/RAILSAPP/PATH/log/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
copytruncate
}
So, How It Works This is fantastically easy. Each bit of the configuration does the following:
- daily – Rotate the log files each day. You can also use weekly or monthly here instead.
- missingok – If the log file doesn’t exist, ignore it
- rotate 7 – Only keep 7 days of logs around
- compress – GZip the log file on rotation
- delaycompress – Rotate the file one day, then compress it the next day so we can be sure that it won’t interfere with the Rails server
- notifempty – Don’t rotate the file if the logs are empty
- copytruncate – Copy the log file and then empty it. Rails keeps writing to the same path. Without this, you need to restart the app so it reopens the new file.
Running Logrotate Since we just wrote this configuration, you want to test it.
To run logrotate manually, just do: sudo /usr/sbin/logrotate -f /etc/logrotate.conf
That’s it.