This is a Nuts & Bolts Article – Tips & Tricks for Developers

Show and tail multiple production remote logs with Capistrano

by Torsten Bühl

Sometimes you want to see what's happening in your production log files right now. You can use log management tools like Loggly or Papertrail for this, but there is also an easy way to stream your log files with Capistrano.

I found a basic implementation here and improved it a bit.

``` ruby


namespace :logs do
desc "tail production log files"
task :tail, roles: :app do
file = fetch(:file, 'production') # uses 'production' as default
trap("INT") { puts 'Interupted'; exit 0; }
run "tail -f #{shared_path}/log/#{file}.log" do |channel, stream, data|
puts # for an extra line break before the host name
puts "#{channel[:host]}: #{data}"
break if stream == :err

# config/deploy.rb
load "config/recipes/logs"

The main difference is the trap("INT") method which is used to abort the execution of the task with control + c. Additionally, file = fetch(:file, 'production') is used to specify the log file you want to tail in the console. fetch checks if there is an argument provided with the -s flag. If not, it uses a default value.

Note: Capistrano handles arguments in a specific way. You cannot check the value of file for nil or set it with the ||= operator.

So how do you run the task? It's pretty easy:

# This will show and tail the production.log
$ cap logs:tail

# This will show and tail the cron.log
$ cap logs:tail -s file=cron

This is useful if you have several log files, for example a cron.log to log your cronjobs.

Any feedback? Just ping me at @tbuehl.

This is a Nuts & Bolts Series post – join the mailing list below to get more tips & tricks.

← Back to Overview

Try our simple and powerful
application error tracking