开发者

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 productio开发者_如何学Gon 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?


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.


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.


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|


I had a problem with the trap("INT") part. While it makes the script exit without errors, the tail processes where still running on the remote machines. If fixed it with this line:

trap("INT") { puts 'Interupted'; run "killall -u myusername tail"; exit 0; }

Not elegant, but working for me.


I use gem capistrano-rails-tail-log and everything is ok. https://github.com/ayamomiji/capistrano-rails-tail-log

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜