Skip to navigation

Logging Rails activity in script/console

Published January 31, 2007 - Updated February 06, 2007

Update: Check out System-wide script/console logging for something a little more convenient.

Just found this nice little nugget:


if "irb" == $0
  ActiveRecord::Base.logger = Logger.new(STDOUT)
end

Chuck that into environment.rb and you’ll get log messages straight into your script/console when interacting with your models. If there was a way to tell if you were in the rails console within .irbrc you could do it system wide.

Can anybody else see a tryactiverecord.com in the making? Why_, you hear that?

Archived comments

Comments were previously allowed on articles. Though no new comments are being accepted you can see the old comments below.

  1. Chad Humphries

    I do something like this in my irbrc:

    
    script_console_running = (ENV.include?('RAILS_ENV') and IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
    rails_running = (ENV.include?('RAILS_ENV') and !IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
    irb_standalone_running = (!script_console_running and !rails_running)
    

    To test it out yourself add the above and the below to your .irbrc file.

    
    puts "script_console_running=#{script_console_running}"
    puts "rails_running=#{rails_running}"
    puts "irb_standalone_running=#{irb_standalone_running}"
    
  2. Tim Lucas

    Sweet, thanks Chad! I’ve used this in my .irbrc and it works a treat.

Thoughts

toolmantim

I’m Tim Lucas, a user experience developer currently in Sydney Australia.

I occasionally write, snap photos, present on various technical topics, tweet my going-ons, share teh codes and post tidbits to the scrapbook.

Most recently I published Simplifying ticket sales on sydneyoperahouse.com (February 16, 2010)

Work with me via Agency Rainford, or shoot an email to and say hello.

Powered by your inner voice