Skip to navigation

System-wide script/console logging

Published February 06, 2007 - Updated February 06, 2007

Last time I mentioned the environment.rb logger hack that helps understand Rails activity in script/console. The only problem was you had to add it to each Rails app’s environment.rb.

Thanks to a hint left by Chad Humpries we now have it system-wide via your .irbrc file.

Chuck the following in your ~/.irbrc file, load up a console and try a User.find(:first).

Update: I’ve slightly rewritten Chad’s code below to prevent a NoMethodError on nil, remove some unnecessary parenthesises and to use && instead of and.


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

if script_console_running
  require 'logger'
  Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end

Look ma… inline logging:


$ ./script/console 
Loading development environment.
>> Person.find(:first)
  Person Load (0.000432)   SELECT * FROM people LIMIT 1
=> #<Person:0x139a2c8 @attributes={...}>

How dandy.

Archived comments

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

  1. Dan Webb

    Nice work. I like that a lot. Big ups to yourself and Chad.

  2. Tim Lucas

    FYI: I’ve updated the code above to fix a small bug with messaging nil.

  3. Lindsay Evans

    Neato.

    Although now I’ll have to mess with script/console to get ANSI colours working properly :(

    BTW, Windows users will need to put .irbrc in their user profile directory (C:\Documents and Settings\[username]), then set the HOME env var to that directory.

  4. Chad Humphries

    Sorry, I had the following at the beginning of my irbrc

    
    IRB.conf[:LOAD_MODULES] = []  unless IRB.conf.key?(:LOAD_MODULES)
  5. MAX HENSCHEL

    TO:> System-wide script/console logging-

    It was a “NICE” try (?!!—)

    I do believe that programmers can (yes: can…) do a lot better. Small white print on pitch black (??) background makes for hard-to read TEXT!!— THANK YOU VERY MUCH DEAR Fellos.—

    Houston, Texas (LONE STAR STATE.)

  6. Nate Klaiber

    Thanks for this tip. I was previously using Jamis Buck’s tip of adding to environemnt.rb, but this makes things much nicer as I want to have it available across all apps.

  7. Marcus

    Great tip! Works like a charm.

Articles

toolmantim

I'm Tim Lucas, a user experience designer and developer currently in Sydney Australia.

I run a web application design and development company Agency Rainford, present on various technical topics, snap the occasional photo, tweet my going-ons, share teh codes and post other tid-bits to the tumble.

Most recently I published LAN hacking with Bananajour (June 11, 2009)

Shoot an email to and say hello.

Powered by tim tams and coffee