2012-08-16 64 views
23

如何防止Rails日誌過多?以下是我的production.log文件中的一個典型跟蹤,許多分支和緩存命中......它在開發中很有用,但我不希望它在我的生產環境中使用。Rails日誌過於冗長

Started GET "/?redirected=true" for 46.193.131.53 at 2012-08-16 18:39:20 +0200 
Processing by HomeController#index as HTML 
    Parameters: {"redirected"=>"true"} 
    Rendered application/_successfully_connected.html.haml (0.8ms) 
    Rendered hotspot_infos/_infos.html.haml (0.4ms) 
    Rendered application/_hotspot_infos.html.haml (1.8ms) 
    Rendered application/_news.html.haml (0.3ms) 
Read fragment views/social-zone-341-directory (0.5ms) 
    Rendered application/_directory.html.haml (2.5ms) 
    Rendered application/_meteo.html.haml (1.1ms) 
    Rendered application/_notifications.html.haml (0.8ms) 
    Rendered application/_like_button.html.haml (0.3ms) 
    Rendered application/_navbar.html.haml (4.2ms) 
    Rendered application/_connection.html.haml (0.5ms) 
    Rendered application/_gallery.html.haml (0.2ms) 
    Rendered application/_search_bar.html.haml (0.4ms) 
    Rendered pictures/_picture_frame.html.haml (0.3ms) 
    Rendered application/_profile_preview.html.haml (1.4ms) 
    Rendered application/_profile_block.html.haml (1.7ms) 
    Rendered application/_menus.html.haml (3.3ms) 
    Rendered application/_left_pane.html.haml (5.5ms) 
    Rendered application/_langs.html.haml (0.8ms) 
    Rendered application/_footer.html.haml (1.9ms) 
    Rendered application/_flash_modal.html.haml (0.1ms) 
    Rendered application/_connection_required.js.erb (0.2ms) 
Completed 200 OK in 159ms (Views: 25.5ms | ActiveRecord: 88.0ms) 

感謝的對你有所幫助

PS:我用Rails 3.2.6

+1

使用** [Lograge](https://github.com/roidrage/lograge)**,最好寶石我找到了清理Rails日誌記錄 – Yarin 2014-01-07 14:51:55

回答

30

在Rails 4將有清理日誌設置:

config.action_view.logger = nil 

爲了實現在Rails 3中,你必須猴子補丁的ActionView:

module ActionView 
    class LogSubscriber < ActiveSupport::LogSubscriber 
    def logger 
     @memoized_logger ||= Logger.new('/dev/null') 
    end 
    end 
end 
+7

FWIW,這個猴子補丁顯然會導致文件句柄泄漏(至少在Rails 3.2.12中)。我建議用'@memoized_logger || = Logger.new('/ dev/null')'替換'Logger.new('/ dev/null')'。這裏的經驗苦澀的聲音。 :) – bheeshmar 2013-11-21 21:23:35

+1

感謝您的報告。我編輯了我的答案。 – sailor 2014-01-02 16:06:20

+1

爲了在日誌級別調試時保持部分日誌記錄的啓用,您可以使用'@memoized_logger || = Rails.logger.debug? ? super:Logger.new('/ dev/null')' – robd 2014-05-22 10:27:21

6

你需要設置你的config.log_level不同。瞭解有關Log Levels

例如,添加以下config/evironments/production.rb

config.log_level = :warn 

此致很可能設置爲:debug:info

+5

問題是當日志級別設置爲警告,我什麼也沒有在我的日誌文件中。我想要的基本信息記錄如「處理由HomeController#索引爲HTML」或「在369毫秒完成200 OK(Views:272.3ms | ActiveRecord:8.1ms)」 – sailor 2012-08-17 09:49:53

3

我把這個在我初始化程序來monkeypatch某些日誌記錄去調試,而不是信息:

module ActionView 
    class LogSubscriber 
    def render_template(event) 
     message = "Rendered #{from_rails_root(event.payload[:identifier])}" 
     message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] 
     message << " (#{event.duration.round(1)}ms)" 
     debug(message) 
    end 
    alias :render_partial :render_template 
    alias :render_collection :render_template 
    end 
end 

module ActionController 
    class LogSubscriber 
    # Use debug logging for read_fragment 
    # %w(write_fragment read_fragment exist_fragment? expire_fragment expire_page write_page).each do |method| 
    %w(read_fragment).each do |method| 
     class_eval <<-METHOD, __FILE__, __LINE__ + 1 
     def #{method}(event) 
      return unless logger.info? 
      key_or_path = event.payload[:key] || event.payload[:path] 
      human_name = #{method.to_s.humanize.inspect} 
      debug("\#{human_name} \#{key_or_path} (\#{event.duration.round(1)}ms)") 
     end 
     METHOD 
    end 
    end 
end