21

Whereas the verbose feature of SQL/ActiveRecord calls is useful most of the time, I would like to turn it off in cases where I have some looping going on.

Is there a way to turn it off?

irb(main):055:0> City.first
  ←[1m←[35mCity Load (1.0ms)←[0m  SELECT `cities`.* FROM `cities` LIMIT 1
=> #<City id: 1, name: "bla bla", state_id: 1, zip: nil, country_id: nil,
created_at: "2011-03-27 14:11:28", updated_at: "2011-08-16 11:14:36", guid: "5PK
fvvz2Gsi">
Abdo
  • 13,549
  • 10
  • 79
  • 98

3 Answers3

25

In console:
Disable:

old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil

Enable:

ActiveRecord::Base.logger = old_logger
Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70
  • 1
    Is there another way of doing this? This is causing errors in code that use logger.debug. I would like to turn off the ones I didn't manually write. – Abdo Nov 01 '11 at 20:58
  • 41
    Another way of doing it, as mentioned in http://stackoverflow.com/questions/7759321/disable-rails-3-1-sql-logging, is `ActiveRecord::Base.logger.level = 1` to turn the logging off (sets log level to info), and `ActiveRecord::Base.logger.level = 0` to turn it back on (sets log level to debug). – Henrik N Jan 17 '12 at 15:59
  • @FloatingRock Only for this session (the current process), I'm 99.9% sure. Try it and report back :) – Henrik N Jan 28 '15 at 13:37
5

In Rails 4 I've been annoyed by ActiveRecord logging SQL statements in the middle of my specs so I disable it by adding this to config/environments/test.rb:

Rails.application.configure do
  # ... 
  config.log_level = :info
end
drewish
  • 9,042
  • 9
  • 38
  • 51
0

You can try to place this code in your rails_spec.rb file.

  config.before do
    ActiveRecord::Base.logger.level = 1
  end

It did the trick for me.

Unkas
  • 3,522
  • 2
  • 19
  • 23