Ruby on Rails Console Logging



If you’ve developed in Rails for a decent amount of time then you’ve most likely needed to use the interactive console: ruby script/console. This has saved me within our production application on many occasions. As you probably know, when using the console in production mode, logging is disabled. Sometimes it’s useful to see the queries generated by ActiveRecord, which brings me to my scenario from yesterday. I badly needed to see what query was being gererated by one of my model methods and stumbled across this snippet of code:

ActiveRecord::Base.logger = Logger.new(STDOUT)

Be sure to restart your console before running this command and when you execute a statement like User.find(1) you’ll be presented with some handy debug info.



Leave a Reply