Gracefully Age Your Rails Logs with LogRotate
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.
\n\nWell, the enormously astute DoBlock-newcomer shaun noticed, after consuming that article, that it made no note of aging default Rails logs, only custom ones.
\n\nSo 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.
\n\nenter logrotate
\n\nNo, 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.
\n\nThe 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.
\n\nAnyway, might as well double check:
\n", nil, "logrotate --help
If you get something that looks like:
\n", nil, "Usage: logrotate [OPTION...] <configfile>\n -d, --debug Don't do anything, just test (implies -v)\n -f, --force Force file rotation\n -m, --mail=command Command to send mail (instead of `/usr/bin/mail')\n -s, --state=statefile Path of state file\n -v, --verbose Display messages during rotation\n\nHelp options:\n -?, --help Show this help message\n --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:
\n", nil, "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.
\n\nanyway...
\n\nSorry 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:
\n", nil, "sudo vim /etc/logrotate.confAssuming, that is, that you're into vim. Which you should be. More on that later.
\n\nOkay, 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\":
\n", nil, "# system-specific logs may be configured here\n\n# Rotate Rails application logs\n/path/to/your/rails/application/log/*.log {\n daily\n missingok\n rotate 7\n compress\n delaycompress\n notifempty\n copytruncate\n}
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:
\n\ndaily - Rotates the log files every day. You could specify weekly or monthly instead.
\n\nmissingok - Don\u2019t issue an error message if log files are missing.
\n\nrotate 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.
\n\ncompress - Compress old versions of log files to save space (uses gzip by default).
\n\ndelaycompress - Delays the compression until the next log rotation. It\u2019s a minor point and probably not strictly necessary, but it makes sure that the log file is truly no longer active before compressing it.
\n\nnotifempty - If the log file is empty, there\u2019s 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.
\n\ncopytruncate - 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.
\n\nso that's the word
\n\nI 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.
\n\nNote 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:
\n", nil, "$ man rotate\n \nNAME\nlogrotate - rotates, compresses, and mails system logs\n\nSYNOPSIS\nlogrotate [-dv] [-f|--force] [-s|--state statefile] config_file ..\n\nDESCRIPTION\nlogrotate 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.\n\n...
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...
\n"]