2011-03-26 69 views
2

我的Rails應用程序每分鐘進行一次健康檢查,除非出現錯誤,否則我想保留這些應用程序。我能夠在application_controller.rb這個設定記錄器爲此在Rails的2.3.5:如何最小化監控請求的日誌記錄?

def logger 
    if params[:__no_logging__] == 'true' && params[:controller] == 'welcome' 
     && params[:action] == 'index' 
    # ignore monitoring requests 
    RAILS_MONITOR_NULL_LOGGER 
    else 
    RAILS_DEFAULT_LOGGER 
    end 
end 

但是,這並不對Rails 3.0.5

我已經能夠正常工作放在一起用的Monkeypatching Rails中before_dispatch和after_dispatch一個新的解決方案::架::急件:

require 'active_support/core_ext/time/conversions' 
module Rails 
    module Rack 
    # Log the request started and flush all loggers after it. 
    class Logger 
     include ActiveSupport::BufferedLogger::Severity 

     def before_dispatch(env) 
     request = ActionDispatch::Request.new(env) 

     #path = request.filtered_path 
     path = request.fullpath 

     if request.path == '/' && request.parameters['__no_logging__'] == 'true' 
      @log_level = logger.level 
      logger.level = Logger::ERROR 
      #logger.level = 3 
     end 

     info 
     "\n\nStarted #{request.request_method} 
      \"#{path}\" " \ 
      "for #{request.ip} at #{Time.now.to_default_s}" 
     end 
     def after_dispatch(env) 
     logger.level = @log_level unless @log_level.nil? 
     ActiveSupport::LogSubscriber.flush_all! 
     end 
     end 
    end 
end 

我把補丁配置/初始化/ monkey_patch.rb

這工作前actly因爲我需要,我沒有看到在日誌中這樣的要求:

http://mydomain.com?__no_logging__=true

但所有其他要求保留日誌不受影響

在但仍有兩個問題:

1.我需要註釋掉:

path = request.filtered_path 

因爲它會導致這個錯誤:

ERROR NoMethodError: undefined method `filtered_path' for #<ActionDispatch::Request:0x105b4c0e8> 
/ce_development/Rails/g3/config/initializers/monkey_patches.rb:52:in `before_dispatch' 
/ce_development/Rails/g3/.bundle/ruby/1.8/gems/railties-3.0.5/lib/rails/rack/logger.rb:12:in `call' 
... 

我現在明白這不是問題。在我使用的Rails 3.0.5中不存在有問題的方法「request.filtered_pa​​th」。我無意中從定義filtered_pa​​th的Rails 3.1.0.beta複製了我的類。 Rails 3.0.5使用request.fullpath,如上所示。

2.我需要註釋掉

logger.level = Logger::ERROR 

因爲它會導致該錯誤:

ERROR NameError: uninitialized constant Rails::Rack::Logger::ERROR 
/ce_development/Rails/g3/config/initializers/monkey_patches.rb:57:in `before_dispatch' 
/ce_development/Rails/g3/.bundle/ruby/1.8/gems/railties-3.0.5/lib/rails/rack/logger.rb:12:in `call' 
... 

我通過添加上述

include ActiveSupport::BufferedLogger::Severity 
這行解決了這個第二個問題

我是新來的猴子補丁,我無法弄清楚如何獲得我的補丁中定義的filtered_pa​​th或Logger :: Error。我嘗試了其他要求,但沒有運氣。

我還想要關於在我的項目中使用這個猴子補丁的健壯性的任何建議。有一個更好的方法嗎?

我知道有些人不相信改變日誌,但我不希望所有這些ping在日誌中,除非在請求期間有錯誤。

+0

我已經解決了我遇到的兩個問題,儘管我仍然很欣賞使用這個猴子補丁和任何改進以使它更加健壯的指導。 – 2011-03-29 18:26:54

回答