#86
Dec 30, 2007

Logging Variables

Have you ever wanted to easily log all variables? Now you can by using some advanced Ruby concepts as shown in this episode.
Download (9.1 MB, 6:37)
alternative download for iPod & Apple TV (7.4 MB, 6:37)

Resources

# models/product.rb
logger.debug_variables(binding)

# config/initializers/logger_additions.rb
logger = ActiveRecord::Base.logger
def logger.debug_variables(bind)
  vars = eval('local_variables + instance_variables', bind)
  vars.each do |var|
    debug  "#{var} = #{eval(var, bind).inspect}"
  end
end

RSS Feed for Episode Comments 12 comments

1. QuBiT Dec 31, 2007 at 00:01

so I've learned one more thing in 2007 about ruby and rails, just great, thx!

thx 4 all in this year and a happy new year 2008 to Ryan and to all those who are watching this!


2. Adrian Dec 31, 2007 at 02:21

It is hard to thank you enough for these wonderful screencasts. Thanks and have a wonderful 2008!


3. Matt Darby Dec 31, 2007 at 07:26

I was just looking for something on this subject last night! One more awesome screencast. Keep it up!


4. taylor Dec 31, 2007 at 12:39

that was sick, nice script-foo. i have always wanted to do what eval does, just didnt know how to do it or what to call it.


5. Forrest (Weiyuan) Cao Dec 31, 2007 at 23:39

Nice episode that demonstrates Ruby knowledge and skills. As Ruby is a great language for DSL, it has great features to expand Rails. Just show us more like that.

Thanks and Happy New year!


6. Eric Berry Jan 02, 2008 at 06:52

Thanks a ton for your screencasts!

Does this only apply to Rails 2.0 or does this also work in 1.8.6?


7. Ryan Bates Jan 02, 2008 at 10:55

@Eric, this should work in Rails 1.2 as well. The only difference is the file in "initializers" directory won't be loaded automatically. You'll have to either manually load it or place it directly in environment.rb.


8. Herryanto Siatono Jan 03, 2008 at 19:46

Ah..nice. Thanks, but I'm getting this error on my setup:

logger_additions.rb:1: warning: toplevel constant Logger referenced by ActiveRecord::Base::Logger

I removed the line:
logger = ActiveRecord::Base.logger

and changed to:

def RAILS_DEFAULT_LOGGER.debug_variables(bind)
...
end

Hope it'll help others with similar issue.


9. Ryan Bates Jan 04, 2008 at 13:34

wow, nice screencast. I love the way you can hack ruby.

I'm using your name because I wondered wether its possible without being the admin, sorry. Just being curious!

[admin edit] Yep, its possible - probably something I should fix...


10. moe Jan 06, 2008 at 23:25

Great podcast. I have been following your podcast for a few weeks now and actually went back and watched all the previous ones. Great job!

I was able to implement the variable logging pretty easily. A couple quick random thoughts that are most likely stupid.

The idea is to quickly view variables but it would be really helpful if they were organized in someway. Maybe by data structure or if they are empty or not. This way a developer could get used to looking in the same place for nil values , hashes, arrays..etc. Dumb thought?


11. Zeck Mar 13, 2008 at 03:06

Greate screencast. I want to quickly learn Rails ? So I need a help. Please contact me ?


12. kino May 04, 2008 at 05:14

Thanks a ton for your screencasts!

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player