Skip to content

Instantly share code, notes, and snippets.

@francisco-rojas
Last active August 29, 2015 14:11
Show Gist options
  • Save francisco-rojas/f48408f81cf32ecd326a to your computer and use it in GitHub Desktop.
Save francisco-rojas/f48408f81cf32ecd326a to your computer and use it in GitHub Desktop.
A basic guide on how to configure log rotation for a Rails app in Ubuntu.

To avoid the logs growing too large logrotation can be configured with Linux's logrotate tool. The logrotation configuration file is located at: /etc/logrotate.d/your_app_name Example of logrotate configuration file for a Rails app (remember to remove the comments!):

# monitor the /home/deploy/path/to/my/app/shared/log/*.log file
/home/deploy/path/to/my/app/shared/log/*.log {
        weekly # rotate weekly
        missingok #  avoids halting on any error and carries on with the next log file
        rotate 52 # 52 days worth of logs would be kept
        compress # compress using gzip
        delaycompress # delays the compression process till the next log rotation
        notifempty # avoid log rotation if the logfile is empty
        copytruncate # makes a backup copy of the current log and then clears the log file for continued writing
                     # using copytruncate option avoids having to restart the rails app after every log rotation
}

To verify if a particular log is indeed rotating or not and to check the last date and time of its rotation, check the /var/lib/logrotate/status file.

cat /var/lib/logrotate/status

Log rotation can also be triggered manually with:

sudo logrotate -v /etc/logrotate.d/your_app_name

Sometimes you want to trigger log rotation even when logrotate thinks the log doesn't need rotation, you can do so by passing the -f option to "force" the rotation:

sudo logrotate -vf /etc/logrotate.d/your_app_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment