Capistrano的尾部生产日志 - 如何阻止它

M. *_*her 9 capistrano ruby-on-rails ruby-on-rails-3

我在几个站点上找到了这个漂亮的代码片段,允许我通过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
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但是,当我读完日志时,我按下Ctrl + C并在我的控制台上产生一个令人讨厌的错误.这不是一个大问题,但我觉得很烦人.我可以做什么,不会产生错误,但任务/尾部/日志查看只是悄然结束?

另外,我对如何分析日志并不熟悉 - 这是否真的是快速查看(远程生产)日志中最新事件的最佳方式,还是有更好的方法?我知道有很多用于日志分析的工具,但我想要一个简单的解决方案来查看最后几个请求,而不是笨重而复杂的东西.我不确定这个Capistrano解决方案是否真的是最佳的.比如,大多数人使用的解决方案是什么?

Szy*_*Jeż 36

试试trap("INT") { puts 'Interupted'; exit 0; }这样:

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
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.