Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tail production log with Capistrano - how to stop it

I found this nifty code snippet on several sites, allowing me to analyze the production log via Capistrano:

desc "tail production log files" 
task :tail_logs, :roles => :app do
  run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
    puts  # for an extra line break before the host name
    puts "#{channel[:host]}: #{data}" 
    break if stream == :err    
  end
end

It works perfectly well, however, when I'm finished reading the logs, I hit Ctrl+C and it produces a nasty error on my console. Not that this is a huge problem, but I find it annoying. What can I do so that no error is produced, but the task/tail/log viewing just quietly ends?

Also, I'm not that familiar with how to analyze logs - is this really the best way to just have a quick look at the most recent events in your (remote production) log, or is there a better way? I know there are a gazillion tools for log analysis, but I want a dead-simple solution to see the last couple requests, not something bulky and complicated. I'm not sure if this Capistrano solution is really optimal though. Like, what's the solution most people use?

like image 657
M. Cypher Avatar asked Mar 07 '11 11:03

M. Cypher


3 Answers

Try trap("INT") { puts 'Interupted'; exit 0; } like this:

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

I hope this helps.

like image 60
Szymon Jeż Avatar answered Oct 23 '22 21:10

Szymon Jeż


This was pretty easy to find on a blog

But here is some code for Capistrano 3

namespace :logs do
  desc "tail rails logs" 
  task :tail_rails do
    on roles(:app) do
      execute "tail -f #{shared_path}/log/#{fetch(:rails_env)}.log"
    end
  end
end

I had issues with the rails_env variable, so i just replaced it, but it might be worth it to you to get it working, so I left it.

like image 45
counterbeing Avatar answered Oct 23 '22 22:10

counterbeing


I made one small change to Jeznet's great answer. If you run capistrano-ext with multiple environments like we do, you can have the RAILS_ENV automatically specified for you:

run "tail -f #{shared_path}/log/#{rails_env}.log" do |channel, stream, data|
like image 45
Greg Benedict Avatar answered Oct 23 '22 22:10

Greg Benedict