Gracefully Age Your Rails Logs with LogRotate

by Mike Zazaian at 2009-10-13 10:42:40 UTC in unix rails

How to use the unix-standard logrotate to automatically maintain outdated logs in your Rails app

no comments no links

I wrote an article a couple weeks back about creating custom logs in ails using the Ruby Logger and subsequently aging those logs over set intervals so they don't become too massive.

Well, the enormously astute DoBlock-newcomer shaun noticed, after consuming that article, that it made no note of aging default Rails logs, only custom ones.

So being the nice guy that I am I decided to help Shaun out and do a bit of research on the topic. The findings are something that EVERY RAILS APP should have in place in some capacity.

enter logrotate

No, this is not a Ruby-only solution, but it seems to be the best and easiest way to configure and age logs in any web application being hosted on a Linux or Unix-like server, Rails apps included.

The first thing you'll probably want to do in setting this up is to make sure that logrotate is by default installed on your OS. If you're using Linux, you should be safe. Same with BSD. Mac users might want to check whether this package exists, as I'm none too sure that you've got it or if it's available to you. I'd appreciate if somebody would confirm or disconfirm this.

Anyway, might as well double check:

logrotate --help

If you get something that looks like:

Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/usr/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation

Help options:
  -?, --help                Show this help message
      --usage               Display brief usage message

..then you're probably in good shape. If you get a command not found error, however, you can just install it on a debian/ubuntu-based system with:

sudo aptitude install logrotate

Cool. That shouldn't be necessary in any situation, as all debian/ubuntu-based distros should have logrotate, but it's still fun to tout how marvelous and simple package management is with such operating systems. Yum users -- your mileage may vary.


Sorry about that, got a bit off track. Now that you're sure that you've got logrotate in place, take a look at the file resting at /etc/logrotate.conf. You'll need to super-user permissions to edit it, so open it with:

sudo vim /etc/logrotate.conf

Assuming, that is, that you're into vim. Which you should be. More on that later.

Okay, now that you're into the file, go head and paste the following code into the bottom under the part that says "system-specific logs may be configured here":

# system-specific logs may be configured here

# Rotate Rails application logs
/path/to/your/rails/application/log/*.log {
  rotate 7

I actually borrowed this particular configuration from Kevin Skoglund at null is love, but it gets you everything that you'd want for a standard Rails Application. He described each of the above options very eloquently (and much more succinctly than man logrotate would give you), so in the interest of not reinventing the wheel, I'll furnish you with his description of the above code:

daily - Rotates the log files every day. You could specify weekly or monthly instead.

missingok - Don’t issue an error message if log files are missing.

rotate 7 - The maximum number of log files to keep. Once you have more than this number, the oldest file will be deleted. I set it to keep seven days worth but feel free to change this number.

compress - Compress old versions of log files to save space (uses gzip by default).

delaycompress - Delays the compression until the next log rotation. It’s a minor point and probably not strictly necessary, but it makes sure that the log file is truly no longer active before compressing it.

notifempty - If the log file is empty, there’s no need to rotate it. You can remove this option if you want to rotate even blank log files; just keep in mind that you may erase a log file that has lots of information to make room for your blank log file.

copytruncate - Makes a backup copy of the current log and then clears the log file for continued writing. The alternative is to use create which will perform the rotation by renaming the current file and then creating a new log file with the same name as the old file. I strongly recommend that you use copytruncate unless you know that you need create. The reason why is that Rails, FastCGI, Mongrel, etc. may still keep pointing to the old log file even though its name has changed and they may require restarting to locate the new log file. copytruncate avoids this by keeping the same file as the active file.

so that's the word

I hope that's what you were looking for Shaun. There are probably ways in which you could call methods from or alter the default Rails logger to age itself as I demonstrated in the custom Ruby logger article, but I doubt that it's any simpler or more comprehensive than this.

Note also that you can adjust the granularity and custom options with a host of other options about which you can read on the logrotate manual page, which you can pull up with:

$ man rotate
logrotate - rotates, compresses, and mails system logs

logrotate [-dv] [-f|--force] [-s|--state statefile] config_file ..

logrotate  is  designed  to  ease  administration  of  systems that generate large numbers of log files.  It allows automatic rotation, compression, removal, and mailing of log files.  Each log file may be handled daily, weekly, monthly, or when it grows too large.


So check that out too. At the very least, lets hope that your app gets bombarded with traffic, and this is an issue you'll need to take care of...

no comments

Comments closed

latest links

Help.GitHub - Multiple SSH keys The article from github help mirroring this process
ones zeros majors and minors ones zeros majors and minors: esoteric adventures in solipsism, by chris wanstrath
ActiveScaffold A Ruby on Rails plugin for dynamic, AJAX CRUD interfaces


register activate reset







doblock focuses on ruby, rails, and all things that can help ruby and/or rails programmers hone their skills.

Techniques, tutorials, news, and even free open-source applications, doblock seeks to fill in the cracks of the ruby/rails blogosphere.

doblock v. 0.10.1 powered by Rails