How to disable the logger in script/console
Asked Answered
P

2

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?

Perpetual answered 5/11, 2010 at 2:45 Comment(0)
N
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
Nonrepresentational answered 5/11, 2010 at 13:9 Comment(1)
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.Horseradish
C
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.

Company answered 5/11, 2010 at 2:50 Comment(3)
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?Perpetual
yeah, I saw that after I posted it. I don't know how to do it just for a few sql lines :(Company
check the scripts folder in you rails app, I think you can turn off the logger for console use.Company

© 2022 - 2024 — McMap. All rights reserved.