Turn off verbose sql/ActiveRecord for Rails 3.1.1
Asked Answered
D

3

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">
Dowitcher answered 1/11, 2011 at 13:11 Comment(1)
Yep, you can turn it off: #7759821Landgraviate
W
25

In console:
Disable:

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

Enable:

ActiveRecord::Base.logger = old_logger
Weathersby answered 1/11, 2011 at 15:2 Comment(3)
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.Dowitcher
Another way of doing it, as mentioned in #7759821, 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).Araucania
@FloatingRock Only for this session (the current process), I'm 99.9% sure. Try it and report back :)Araucania
M
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
Marin answered 29/11, 2015 at 20:22 Comment(0)
C
1

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.

Chlori answered 27/11, 2021 at 1:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.