Skip to content

Instantly share code, notes, and snippets.

@pvdb
Created July 16, 2010 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvdb/478547 to your computer and use it in GitHub Desktop.
Save pvdb/478547 to your computer and use it in GitHub Desktop.
#
# ActiveRecord SQL Logging on the Rails Console - revisited/updated
# -----------------------------------------------------------------
#
# If you're living on the edge ("edge Rails", that is) and would like to
# log all SQL statements executed by ActiveRecord to STDOUT when you're
# in a Rails console, then the ConsoleLogSubscriber class is for you!
#
# Notes:
#
# (1) integration into ".irbrc" left as an exercise to the reader
# (2) at present (2010-07-16) this is only compatible with edge Rails
# (3) but I expect this will work in the next Rails 3.0 beta or RC release
# (4) this replaces the MultiLogger class: http://gist.github.com/478307
#
module ActiveRecord
class ConsoleLogSubscriber < ActiveRecord::LogSubscriber
def initialize
@logger = Logger.new(STDOUT)
super
end
def sql(event)
super
end
def logger
@logger
end
end
end
ActiveRecord::ConsoleLogSubscriber.attach_to :active_record
@pvdb
Copy link
Author

pvdb commented Jul 16, 2010

ad note (4): this replaces the MultiLogger class

For older versions of Rails 3.0 (ie. "3.0.0.beta4" and before) which don't implement the LogSubscriber class, the MultiLogger class can be used instead:

http://gist.github.com/478307

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