Skip to content

Instantly share code, notes, and snippets.

@indigoviolet
Last active December 26, 2017 17:28
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 indigoviolet/9ca5a210b8dab67948eac48557151e00 to your computer and use it in GitHub Desktop.
Save indigoviolet/9ca5a210b8dab67948eac48557151e00 to your computer and use it in GitHub Desktop.
fin blog embed
# 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