Rotating Rails Production Logs with LogRotate Discussion
Would it be good to backup the compressed log files off production server? Can we use backup gem for just that?
You could. I rarely have the need for going back to logs over 2 weeks old, so I don't keep them around. If you need to, you can always sync those somewhere.
To handle random spurts of excessive log generation tuck in 'maxsize [size]' option too. Maxsize ensure logrotation both weekly/daily etc. and also out of turn if the logfiles increase beyond a specified [size].
Hello Chris, Is there any way we can rename the file concatenating the date it rotates rather then "1".
Looks like you can if you add the "dateext" option. More details here: http://www.thegeekstuff.com...
Just want to share, If you want older files in a seperate directory then you may use "olddir" parameter in it; like :
/var/log/news/* {
monthly
rotate 2
olddir /var/log/news/old
missingok
compress
}
I have been thinking of implementing this since long back - finally implemented after reading this article.
Thanks for summing up this very well.
The logrotate.conf file includes everything in /etc/logrotate.d/ (assuming you have the line `include logrotate.d/`), so i'm curious why you didn't choose to create a file like /logrotate.d/rails-app and include that above loging setup.
Add `this takes barely 5 minutes to read and complete` ;-). Concise badge. Also, your article should have a date on them (both created and updated); provides added insights as to potential gotchas from framework changes over time.
why not create a application specific log file and place it in the /etc/logrotate.d folder?
It will be application specific, but stays in the app's directory so you can keep it nice and organized next to the code.
Thanks for the great guide.
Just in case anyone else gets this error:
error: skipping "$RAILS_DIR/log/development.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
I was able to fix it by instructing the logrotate to run as the same user as my rails app (in my case deploy) instead of running as root:
$RAILS_DIR/log/*.log {
su deploy deploy
daily
missingok
rotate 7
compress
delaycompress
notifempty
copytruncate
}
Does one need to take any special care about race conditions between log rotation and the logger writing to the file being rotated?
- Already configed and run
sudo /usr/sbin/logrotate -f /etc/logrotate.conf
. - ls to log folder and see:
development.log development.log.1 production.log production.log.1
Why can't I see the compressed log (production.log.1.gz)?
Below are the configuration with log rotate:
/home/ubuntu/project_name/log/*.log { daily missingok rotate 30 compress delaycompress notifempty copytruncate }
Thanks beforehand for your response.
It's because of the delaycompress
line, which says to only compress on the second iteration. If you run sudo /usr/sbin/logrotate -f /etc/logrotate.conf
(on non empty logfiles) the logs will be compressed