Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rotating Rails Production Logs with LogRotate Discussion

Chris Oliver asked in General

Would it be good to backup the compressed log files off production server? Can we use backup gem for just that?

Reply

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.

Reply

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

Reply

Solid!

Reply
k092017 Mohammad Ali k092017 Mohammad Ali

Hello Chris, Is there any way we can rename the file concatenating the date it rotates rather then "1".

Reply

Looks like you can if you add the "dateext" option. More details here: http://www.thegeekstuff.com...

Reply
Shadman Jamil Shadman Jamil

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
}

Reply

I have been thinking of implementing this since long back - finally implemented after reading this article.
Thanks for summing up this very well.

Reply
Drew Verlee Drew Verlee

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.

Reply

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.

Reply

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

Reply

Thank you so much for this post! :)

Reply

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

Reply
Drew Verlee Drew Verlee

why not create a application specific log file and place it in the /etc/logrotate.d folder?

Reply

It will be application specific, but stays in the app's directory so you can keep it nice and organized next to the code.

Reply
Matt Bearman Matt Bearman

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
}

Reply

Does one need to take any special care about race conditions between log rotation and the logger writing to the file being rotated?

Reply
  1. Already configed and run sudo /usr/sbin/logrotate -f /etc/logrotate.conf.
  2. 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.

Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.