Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I turn off Rails SQL logging in test?

For some reason (probably an updated gem) Rails is logging all my SQL commands now. I run autotest and they are being spammed during tests also. How do I turn it off?

I tried add this to config/environments/test.rb but it didn't work. logger was already nil.

# ActiveRecord::Base.logger = nil
# ActiveRecord::Base.logger.level = 1

Rails 4.0.0

like image 369
Chloe Avatar asked Feb 15 '14 02:02

Chloe


3 Answers

In case someone wants to actually knock out SQL statement logging (without changing logging level, and while keeping the logging from their AR models):

The line that writes to the log (in Rails 3.2.16, anyway) is the call to 'debug' in lib/active_record/log_subscriber.rb:50.

That debug method is defined by ActiveSupport::LogSubscriber.

So we can knock out the logging by overwriting it like so:

module ActiveSupport
  class LogSubscriber
    def debug(*args, &block)
    end
  end
end
like image 61
fakeleft Avatar answered Oct 01 '22 17:10

fakeleft


Another thing you can do is call this code at runtime, it doesn't need to be in a config file.

For example, if you put in your specific test case

# test/functionals/application_controller_test.rb for example
ActiveRecord::Base.logger = nil

It would work just as well, and this way you can toggle it at runtime. Useful if you only want to stifle a few lines of code or a block.

like image 25
OneChillDude Avatar answered Oct 02 '22 17:10

OneChillDude


Ok I found it. This worked:

config.after_initialize do
  ActiveRecord::Base.logger = nil
end
like image 41
Chloe Avatar answered Sep 29 '22 17:09

Chloe