Monday, March 29, 2010

Better Looking Console Output

I was listening recently to one of Ryan Bates great screencasts where he briefly mentions how to setup script/console to display better looking output after querying ActiveRecord Models. This comes in handy when trying to debug certain things.

We start by looking at what the default output looks like.

jleal@ubuntu-ruby:~/swapme$ ruby script/console
Loading development environment (Rails 2.3.2)

>> User.find(:first, :select => "id, email, created_at, updated_at")

=> #<User id: 1, email: "sell@gmail.com", created_at: "2010-03-29 04:51:37", 
    updated_at: "2010-03-29 04:51:37">


Now lets see if we can improve things a bit. In order to accomplish this we're going to do the following things:

  • Install and enable the 'hirb' ruby gem.
  • Redirect the ActiveRecord logging to display on the console.

Hirb gem


Hirb is described on github as:
Hirb provides a mini view framework for console applications and uses it to improve irb’s default inspect output.
Hirb presents outputs for several objects much prettier than their default. You can even use hirb to create custom outputs for those objects hirb has not defined.


First we install the gem.

jleal@ubuntu-ruby:~$ sudo gem install hirb
Successfully installed hirb-0.3.1
1 gem installed

Next we load the gem in the console.

jleal@ubuntu-ruby:~/swapme$ ruby script/console
Loading development environment (Rails 2.3.2)
>> require 'hirb'
=> []
>> Hirb.enable
=> true
>> 



Our output looks allot nicer.

>> User.find(:first, :select => "id, email, created_at, updated_at")
+----+---------------------------+-------------------------+--------------+
| id | email          | created_at              | updated_at              |
+----+---------------------------+-------------------------+--------------+
| 1  | sell@gmail.com | 2010-03-29 04:51:37 UTC | 2010-03-29 04:51:37 UTC |
+----+---------------------------+-------------------------+--------------+
1 row in set
>> 


Redirecting ActiveRecord Logging.


Finally, we'll redirect ActiveRecord logging to display on the console. This is done with one line of code. Remember to reload so it takes effect.

>> ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #<Logger:0xb6fb2b04 @formatter=nil, @level=0, 
@default_formatter=#<Logger::Formatter:0xb6fb2adc @datetime_format=nil>, 
@progname=nil, @logdev=#<Logger::LogDevice:0xb6fb2ab4 
@mutex=#<Logger::LogDevice::LogDeviceMutex:0xb6fb2a8c @mon_waiting_queue=[], 
@mon_entering_queue=[], @mon_count=0, @mon_owner=nil>, 
@dev=#<IO:0xb7807570>, @shift_size=nil, @shift_age=nil, @filename=nil>>
>> reload!
Reloading...
=> true

Now let's see how things look.

>> User.find(:first, :select => "id, email, created_at, updated_at")
  User Load (15.7ms)   SELECT id, email, created_at, updated_at FROM "users" LIMIT 1
+----+---------------------------+-------------------------+--------------+
| id | email          | created_at              | updated_at              |
+----+---------------------------+-------------------------+--------------+
| 1  | sell@gmail.com | 2010-03-29 04:51:37 UTC | 2010-03-29 04:51:37 UTC |
+----+---------------------------+-------------------------+--------------+
1 row in set

Very nice. We now have a more readable output plus we can see the actual SQL that was generated.

Hope you enjoyed this!