12

In my .irbrc file I require 'logger' to allow me to see the SQL executed when querying ActiveRecords all while in the script/console.

My question is, how do I temporarily turn off the logger so it doesn't display the SQL just for a few ActiveRecord queries?

Coderama
  • 11,050
  • 12
  • 44
  • 58

2 Answers2

15

To toggle logging in script/console here's what I use:

def show_log
  change_log(STDOUT)
end

def hide_log
  change_log(nil)
end

def change_log(stream, colorize=true)
  ActiveRecord::Base.logger = ::Logger.new(stream)
  ActiveRecord::Base.clear_all_connections!
  ActiveRecord::Base.colorize_logging = colorize
end
cldwalker
  • 6,155
  • 2
  • 27
  • 19
  • 6
    Cool, I like this! Though for Rails 3, you need to take out the last line of the change_log method. Also, returning a nil for that method will give a cleaner "toggle" output. – janechii Sep 26 '11 at 22:25
1

you can turn off your logger by running in production mode or by adjusting your logger file in development.rb environment file in your config directory if you are in fact running in development on your localhost.

thenengah
  • 42,557
  • 33
  • 113
  • 157
  • Thanks for the reply. But I am after a temporary solution while in the script/console. e.g. I am in the script/console and am working away with the need for the logger, but then I want to stop the logger from displaying the SQL just for one AR query I am about to run. How would I turn off the logger, and then turn it back on all while in script/console? – Coderama Nov 05 '10 at 02:53
  • yeah, I saw that after I posted it. I don't know how to do it just for a few sql lines :( – thenengah Nov 05 '10 at 02:56
  • 1
    check the `scripts` folder in you rails app, I think you can turn off the logger for `console` use. – thenengah Nov 05 '10 at 02:58