All threads / Rotating Rails Production Logs with LogRotate Discussion
Ask A Question


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?

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:

k092017 Mohammad Ali ·

Thanks work perfect!!

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/* {
rotate 2
olddir /var/log/news/old

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

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.

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! :)

Naqash Ahmad ·

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

Drew Verlee ·

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.

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

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

  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.

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

Join the discussion

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.