Archiving your logs to Amazon S3 is a simple way to
backup your log files. They'll be on S3 if you ever need them and you'll free up
space on you servers. You could use a logging service, but if you're like me you
won't be using the fancy interface. Ruby has a built in solution to log rotation.
The only other things you need is the rake task to archive the files to S3 and a
Systemd Timer
(or use the crontab).

The Logger

The Rails ActiveSupport::Logger
is a wrapper over Ruby's built in Logger.
The Ruby logger has an option for rotating the logs based on size or time.

To add support for Rails we just modify the
environment file (config/environments/production.rb) and pass the same
arguments to ActiveSupport::Logger which will then be passed on to the standard
Ruby Logger.

Rails.application.configure do
# Your production configuration.
# Roteate logs when it is 100MB in size and keep 100.
config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 100, 104857600)
end

The configs.paths['log'].first will grab the file used for logging based off
the current environment (eg. log/production.log).

The Rake Task

The rake task is what will clean up any of the rotated log files and archive
them to S3. It looks for all rotated log files and then uses s3cmd
to upload them to an Amazon S3 bucket. Once completed, the task removes the
rotated logs. The task uses the AWS credentials in a configuration file
(config/application.yml in this case).

The Systemd Timer

The last piece is to setup the periodic task to archive all rotated logs. The
crontab is an alternative if you aren't familiar with Systemd. Most systems are
now using systemd, and if
you are using systemd units
to controll your app, it's the way to go.

There are two files to create to setup the timer, the service file and the timer
file. Place the following into the /usr/lib/systemd/system/APPNAME-log-archiver.timer.

[Unit]
Description=APPNAME Log Archiver
[Timer]
# How often you want the logs to be archived
# See: http://www.freedesktop.org/software/systemd/man/systemd.time.html
OnCalendar=daily
[Install]
# Use WantedBy to add as a dependency to the application
# WantedBy=app.target

Then create the related service for the timer /usr/lib/systemd/system/APPNAME-log-archiver.service.

[Unit]
Description=APPNAME Log Archiver
After=network.target
[Service]
# I tend to have a conf file for my Rails App Systemd Servcie for specifing
# the environment to run in. Depends on how your setup is though.
# .include /usr/lib/systemd/system/core.target.conf
User=APPUSER
Group=APPUSER
# The working directory for your Rails app, my apps are all served
# from the /srv/APPNAME/current directory (Capistrano setup)
WorkingDirectory=/srv/APPNAME/current
ExecStart=/usr/bin/bundle exec rake log:archive

Enable

Once you deploy the app you can then enable the systemd timer on each server.