How to prevent Rails from Action View logging in production
Asked Answered
C

3

6

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
Complicacy answered 20/10, 2012 at 3:28 Comment(0)
A
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!

Abbotson answered 4/5, 2013 at 0:25 Comment(4)
Interesting! How would you do the opposite after toggling off? changing unsubscribe to subscribe doesn't workMathura
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.Scampi
@ThiagoJackiw Consider updating your answer to incorporate the above comment regarding Rails 5+.Intolerable
Did not work in Rails 7.0.8Claribel
A
1

You can directly turn off action_view logger.

Rails.application.configure do
  config.action_view.logger = nil
end
Anaptyxis answered 19/5, 2020 at 14:29 Comment(4)
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.Maintenance
@Maintenance It still works in Rails 6.0.3.4. The code should put in an initializer e.g. config/initializers/action_view.rb.Anaptyxis
Yep. I just put it in config/application.rb or config/environments/production.rb. Worked fine in either place.Maintenance
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 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.Maintenance
C
0

For (at least) Rails 7.0.8

config.action_view.logger = nil
Claribel answered 23/11, 2023 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.