5

In rails 3.2.0,is it possible to turn off rails logging for rendering of views in ActionView:: LogSubscriber in production environment.

Currently only way i found to supress is to monkey patch it and increase the log level to debug in the below way. Is there a better way to do this or any configuration?

 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
Thiago Jackiw
  • 827
  • 6
  • 10
Phani
  • 1,704
  • 3
  • 13
  • 18

2 Answers2

23

ActionView uses ActiveSupport::Notifications and ActiveSupport::LogSubscriber to instrument its events, and to silence it from the logs is as simple as including the following in your environment file:

%w{render_template render_partial render_collection}.each do |event|
  ActiveSupport::Notifications.unsubscribe "#{event}.action_view"
end

Cheers!

Thiago Jackiw
  • 827
  • 6
  • 10
  • Interesting! How would you do the opposite after toggling off? changing `unsubscribe` to `subscribe` doesn't work – Cruz Nunez May 04 '18 at 16:21
  • 3
    This needs to be adjusted in Rails 5+ by wrapping it with an `ActiveSupport::on_load :action_view do` to ensure it is ran AFTER actionview has been registered. – Urkle May 24 '19 at 18:32
  • @ThiagoJackiw Consider updating your answer to incorporate the above comment regarding Rails 5+. – moveson Feb 16 '20 at 21:24
1

You can directly turn off action_view logger.

Rails.application.configure do
  config.action_view.logger = nil
end
swordray
  • 833
  • 9
  • 21
  • This does seem to work for me nicely in Rails 6.0.3.4. It's possible this wasn't available when accepted answer was accepted? This way seems simpler than the accepted answer, not sure if there are any downsides to doing it this way, or if what it disables isn't exactly the same as in accepted answer. – jrochkind Oct 28 '20 at 18:48
  • @jrochkind It still works in Rails 6.0.3.4. The code should put in an initializer e.g. `config/initializers/action_view.rb`. – swordray Oct 29 '20 at 08:58
  • Yep. I just put it in `config/application.rb` or `config/environments/production.rb`. Worked fine in either place. – jrochkind Oct 30 '20 at 12:09
  • Unfortunately, this ended up causing errors in my app that look like `NoMethodError: undefined method `debug' for nil:NilClass`, as some third-party code tries to call `logger.debug`, and gets the `nil` we have set as `logger`. Very confusing what Rails is expecting here. I may try using the third-party https://github.com/karafka/null-logger instead of `nil`. While Rails Guide *does* suggest setting to `nil`, this may explain why more complicated solution in accepted answer is preferred by some. – jrochkind Nov 02 '20 at 16:54