且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Ruby on Rails 3.1中禁用资产管道(链轮)消息的日志记录?

更新时间:2022-03-19 00:42:00

将以下代码放入config/initializers/quiet_assets.rb

if Rails.env.development?
  Rails.application.assets.try(:logger=, Logger.new('/dev/null'))
  Rails::Rack::Logger.class_eval do
    def call_with_quiet_assets(env)
      previous_level = Rails.logger.level
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/assets/}
      call_without_quiet_assets(env)
    ensure
      Rails.logger.level = previous_level
    end
    alias_method_chain :call, :quiet_assets
  end
end

已更新:它现在也适用于Ruby on Rails 3.2(先前的尝试已修复before_dispatch,现在我们打算使用根机架call)

Updated: It now works for Ruby on Rails 3.2 too (previous attempt fixes before_dispatch, and now we're going for the root rack call instead)

更新:来自@macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735

Update: A proper Rack middleware solution (instead of fragile alias_method_chain) from @macournoyer https://github.com/rails/rails/issues/2639#issuecomment-6591735