-
-
Save indigoviolet/9ca5a210b8dab67948eac48557151e00 to your computer and use it in GitHub Desktop.
fin blog embed
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
# Don't mess with vanilla pry | |
if defined?(Rails) | |
# ,----------------------------------------------------------- | |
# | !debug : ActiveRecord query logging + set log level :debug | |
# `----------------------------------------------------------- | |
Pry::Commands.block_command('!debug', 'Enable debug logging. Pass in an optional number of backtrace lines to print out.') do |*args| | |
num_debug_lines = args.length > 0 ? args[0].to_i : 1 | |
# Turn on Rails debug logging | |
Rails.logger.level = :debug | |
# Point the activerecord logger at STDOUT | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# initialize a counter | |
ActiveRecordQueryCounter.instance.reset | |
ActiveRecordQueryCounter.instance.set_trace_lines(num_debug_lines) | |
# subscribe to query events and print out the trace each time | |
ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload| | |
if payload[:name] != 'SCHEMA' | |
ActiveRecordQueryCounter.instance.trace.each { |line| ActiveRecord::Base.logger.debug(line) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment