#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 -64 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. Slangasaurus Dec 11, 2008 at 07:56

If you're getting a bunch of unnecessary instance variables such as

@_cookies or @url

Then you can filter out the responses you don't want like this:

http://pastie.org/336813


13. new jordans Aug 01, 2010 at 23:34

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


15. logo design Aug 09, 2010 at 23:34

I have been looking for all this, I came here from google i was searching logging variables


16. free directory list Aug 11, 2010 at 22:24

Keep it short and simple. This is what you do!


17. authentic nike shoes Aug 14, 2010 at 02:16

Greate screencast. I want to quickly learn Rails ? So I need a help. Hope it'll help others with similar issue.


18. supra tk society Aug 18, 2010 at 18:54

good job,good article


19. louis vuitton shoes Aug 26, 2010 at 20:55

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


20. snow boots Aug 31, 2010 at 00:40

i have always wanted to do what eval does, just didnt know how to do it or what to call it.


21. Cheap Supra Shoes Sep 01, 2010 at 01:57

Good place to learn new things


22. GHD Hair Straighteners Sep 01, 2010 at 01:58

I would like to learn more from this website.


23. GHD Australia Sep 01, 2010 at 01:59

It's really useful to me.


24. louis vuitton sunglasses Sep 01, 2010 at 21:16

Wow this is a great resource.. I’m enjoying it.. good article

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
Give Back to Open Source