Created
July 16, 2010 12:24
-
-
Save pvdb/478307 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# ActiveRecord SQL Logging on the Rails Console | |
# --------------------------------------------- | |
# | |
# Various blog posts out there show you how to log all SQL statements | |
# executed by ActiveRecord to STDOUT when you're in a Rails console: | |
# | |
# * http://robots.thoughtbot.com/post/159806033/irb-script-console-tips | |
# * http://weblog.jamisbuck.org/2007/1/8/watching-activerecord-do-it-s-thing | |
# * http://toolmantim.com/thoughts/system_wide_script_console_logging | |
# * http://maintainablesoftware.com/articles/rails_logging_tips | |
# | |
# The drawback is that these approaches *replace* the existing logger, | |
# meaning the SQL statements are no longer logged to log/development.log | |
# (assuming "development" is the Rails environment for your Rails console) | |
# | |
# also, they aren't completely kosher when it comes to things like | |
# severity (ie. log levels), buffering behavior and progname support | |
# | |
# this may be acceptable in most situations, but a slightly better, | |
# albeit more cumbersome, approach is implemented by the MultiLogger | |
# class, the definition and usage of which is illustrated below. | |
# | |
# Notes: | |
# | |
# (1) integration into ".irbrc" left as an exercise to the reader | |
# (2) (caveat emptor) compatible with Rails 2.{0,1,2,3}.x and 3.0.0.beta{1,2,3,4} | |
# (3) in newer versions of Rails, MultiLogger can be replaced with LogSubscriber | |
# (4) something similar might (?) be achieved with http://github.com/burke/multilogger | |
# | |
module ActiveSupport | |
class MultiLogger < BufferedLogger | |
def initialize(original_logger, additional_logger, level = DEBUG) | |
super(additional_logger, level) | |
@original_logger = original_logger | |
end | |
def add(severity, message = nil, progname = nil, &block) | |
super(severity, message, progname, &block) | |
@original_logger.add(severity, message, progname, &block) | |
end | |
end | |
end | |
original_logger = ActiveRecord::Base.logger | |
ActiveRecord::Base.logger = ActiveSupport::MultiLogger.new(original_logger, STDOUT, original_logger.level) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ad note (3): in newer versions of Rails,
MultiLogger
can be replaced withLogSubscriber
The next beta or RC release of Rails 3.0 (ie. post "3.0.0.beta4") will introduce the ActiveSupport::LogSubscriber class as a mechanism for subscribing to very specific Rails notifications, e.g. "SQL notifications" issued by ActiveRecord.
The ActiveSupport::Notifications mechanism is intended to provide an instrumentation API for Rails, but allows us to implement "ActiveRecord SQL Logging on the Rails Console" slightly differently, as illustrated in the following gist:
http://gist.github.com/478547
Lemme know how you get on using these two classes,
ActiveSupport::MultiLogger
andActiveRecord::ConsoleLogSubscriber
... happy logging! :-)