Skip to main content

Rotating Rails Production Logs with LogRotate Discussion

General • Asked by Chris Oliver

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


k092017 Mohammad Ali

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

k092017 Mohammad Ali

Thanks work perfect!!


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.

Oh, I see what you mean. You could do that just as well, no particular reason either way.


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.

er... and testing rotating a second time needs production traffic to run if `notifempty` is set !


Thank you so much for this post! :)


can i set the time for hourly backup manually ??
like at every hour's past 15 minutes ??


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?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.