Logging From Your Rails Models

update 2010/10/16: RAILS_DEFAULT_LOGGER is deprecated. I’m using ActiveRecord::Base.logger now as in:

ActiveRecord::Base.logger.debug "value of x is #{x}"

read on for the older solution…

I just spent five minutes figuring out how to log from a model class in Rails (or any class that isn’t a controller or view). Y’see, there is no “logger” method on an ActiveRecord for instance.  Once again Robby to the rescue with an oblique mention of the RAILS_DEFAULT_LOGGER global. I love that guy.

Related: I wonder what Jamis circa ’05 was talking about when he said:

Note: logging is a bad example here, because Rails already has very good support for logging. Furthermore, recent Rails releases support a service keyword, for declaring and using system-global services in the same manner described here.

Huh? Service keyword?

This entry was posted in Ruby on Rails. Bookmark the permalink.

2 Responses to Logging From Your Rails Models

  1. re: service keyword

    I’m not sure what he was talking about either.

    However, The RAILS_DEFAULT_LOGGER is quite handy sometimes. :-)

  2. sakthi says:

    Thanks for the tip. It helped me isolate a performance problem to a single line of code in a model by logging.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s