Rails日志太冗长了

sai*_*lor 24 ruby-on-rails ruby-on-rails-3

如何防止Rails记录太多?这是我的production.log文件中的典型跟踪,许多部分,缓存命中......它在开发中很有用,但我不希望它在我的生产环境中.

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

谢谢你的帮助

PS:我正在使用Rails 3.2.6

sai*_*lor 31

在Rails 4中,将有一个清理日志的设置:

config.action_view.logger = nil
Run Code Online (Sandbox Code Playgroud)

要在Rails 3中实现这一点,你必须修补ActionView:

module ActionView
  class LogSubscriber < ActiveSupport::LogSubscriber
    def logger
      @memoized_logger ||= Logger.new('/dev/null')
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

  • FWIW,这个猴子补丁显然导致文件句柄泄漏(至少在Rails 3.2.12中).我建议用`@memoized_logger || = Logger.new('/ dev/null')`替换`Logger.new('/ dev/null')`.这里经验苦涩的声音.:) (7认同)

dee*_*our 6

你需要以config.log_level不同的方式设置.了解日志级别.

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

config.log_level = :warn
Run Code Online (Sandbox Code Playgroud)

您的可能设置为:debug:info.

  • 问题是当日志级别设置为警告时,我的日志文件中什么都没有.我希望基本信息记录如"由HomeController处理#ssdex为HTML"或"在369ms完成200 OK(视图:272.3ms | ActiveRecord:8.1ms)" (5认同)