Every now and then I’m in a situation where I want to see what SQL calls ActiveRecord is making without tailing the log file or jumping back and forth between a console and text editor. Recently, I’ve been working on crafting custom reports that query over rather large sets of data, so logging to the console is a little overkill, but looking at color entities in a text editor is rather distracting. Most solutions give you a nice method to switch to any IO stream, but none for switching back to the default logger. The method I use allows one to switch between the Rails default logger and STDOUT and gives the option to turn colorization on and off. In ~/.irbrc add the following lines:
if ENV.include?('RAILS_ENV') def stdout_logging(colorize = true) ActiveRecord::Base.logger = Logger.new(STDOUT) ActiveRecord::Base.clear_active_connections! ActiveRecord::Base.colorize_logging = colorize return nil end def rails_logging(colorize = true) ActiveRecord::Base.logger = RAILS_DEFAULT_LOGGER ActiveRecord::Base.clear_active_connections! ActiveRecord::Base.colorize_logging = colorize return nil end end