Skip to content

Instantly share code, notes, and snippets.

@pvdb
Created July 16, 2010 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pvdb/478307 to your computer and use it in GitHub Desktop.
Save pvdb/478307 to your computer and use it in GitHub Desktop.
#
# 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)
@pvdb
Copy link
Author

pvdb commented Jul 16, 2010

ad note (3): in newer versions of Rails, MultiLogger can be replaced with LogSubscriber

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 and ActiveRecord::ConsoleLogSubscriber... happy logging! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment