如何防止Rails从Action查看生产中的日志记录

Pha*_*ani 4 actionview ruby-on-rails-3.2

在rails 3.2.0中,是否可以在生产环境中关闭用于在ActionView :: LogSubscriber中呈现视图的rails日志记录.

目前只有我发现supress的方法是对它进行修补并通过以下方式增加日志级别进行调试.有没有更好的方法来执行此操作或任何配置?

 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 << (" (%.1fms)" % event.duration)
            debug(message)
       end
          alias :render_partial :render_template
          alias :render_collection :render_template
     end
  end
Run Code Online (Sandbox Code Playgroud)

小智 17

ActionView使用ActiveSupport :: NotificationsActiveSupport :: LogSubscriber来检测其事件,并使其从日志中静音就像在环境文件中包含以下内容一样简单:

%w{render_template render_partial render_collection}.each do |event|
  ActiveSupport::Notifications.unsubscribe "#{event}.action_view"
end
Run Code Online (Sandbox Code Playgroud)

干杯!

  • 这需要在 Rails 5+ 中通过用`ActiveSupport::on_load :action_view do` 包裹它来调整,以确保它在 actionview 注册后运行。 (3认同)