The Rails way
As per documentation you can configure Rails with a logger conforming to interface of Log4r or Ruby default Logger class interface. And it turns out Ruby default Logger has build-in ability to rotate logs. Let’s create logger which will rotate your logs every 3 days or after it grows upto 10 Mb:
To configure Rails to rotate logs add this code to
I am assuming you use Ubuntu to run your Rails app.
Firstly, install logrotate utility:
sudo apt-get update && sudo apt-get install logrotate
Then, create new logrotate config file for your app:
sudo nano /etc/logrotate.d/sample-app
It should look lo this:
Try to rotate your logs by running:
sudo logrotate /etc/logrotate.d/sample-app.
cat /var/lib/logrotate/status to see which logs got rotated and when.
Now you need to specify cron job to rotate your logs every day automatically.
sudo nano /etc/cron.daily/logrotate to create cron config for logrotate.
Cron config should look like so:
It seem now you all set, your Rails app logs should be rotated on daily basis.