A common requirement in managing web apps is to rotate your application logs on a daily basis or based on log file size for heavily used apps. Most web frameworks have such as Rails ship with a default logging library that support some form of log rotation. However, in the web apps we’ve shipped for customers, we’ve preferred to rely on the standard linux utility logrotate for the following reasons.
1. It is a proven tool and has all the features that you typically need.
2. You need to learn only one tool which is ubiquitous on all linux systems as opposed to learning how to do it for each logging library.
Assuming your Rails application logs are stored in a file named production.log, here is what we need to do to configure logrotate.
cd /etc/logrotate.d vi hello_world # copy this content over <path_to_rails_app>/log/production.log { daily size 100M missingok notifempty rotate 4 compress delaycompress copytruncate nodateext }
1. daily – tells log rotate to rotate the log files daily.
2. size – If the size option is specified, that takes higher precedence. In other words, when log rotate runs with the above configuration, logrotate will rotate the log file if its size exceeds 100 MB
3. missingok – do not abort or throw errors if the log file is missing and continue processing the other log files.
4. notifempty – do not rotate empty log files
5. rotate 4 – This means a particular log file would be rotated 4 times before it is removed.
6. compress – Rotated log files would be gzipped to save on disk space.
7. delaycompress – This is a cool option that you can use along with compress. This delays the compression of the rotated log file by one cycle. This is useful when the application cannot write to the new logs immediately and continues writing to the old log. Delaying compression would ensure the log messages are received in the rotated log file.
8. copytruncate – This is a very useful option for Rails apps. Without this option, you would have to restart the rails server to enable it to write to the new log file instead of the rotated one. copytruncate creates a copy of the log file to be rotated and truncates the original so that the application can continue to write to the logs without restart.
9. nodateext – Do not add the date to the file name while archiving, instead use numeric versioning. This is the preferred option if you are going to be rotating the log file more than once in a day.
By default, logrotate runs once a day. If you want logrotate to run more than once (you will needs this when your rotation is based on size), you will have to configure a cron task to run, say every hour
0 */1 * * * /usr/sbin/logrotate /etc/logrotate.conf -s ~/logrotate/status
The above snippet assumes you have a logrotate directory under your home, so that logrotate can write the results of the logrotate operation to the status file.
Leave a Reply