Skip to content

Instantly share code, notes, and snippets.

@maxim
Created August 8, 2012 05:58
Show Gist options
  • Select an option

  • Save maxim/3292581 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/3292581 to your computer and use it in GitHub Desktop.
with_query_trace method for logging SQL call traces in specific parts of code, without restarting server
if Rails.env.development?
module Kernel
def with_query_trace
Rails.backtrace_cleaner.add_silencer do |line|
line.include?('query_trace.rb')
end
callback = lambda do |*args|
bt = Rails.backtrace_cleaner.clean(caller[1..-1])
ignored = %w(
postgres_prepared_statements_fix
config/environment.rb
)
cache_hit = args.last[:name].include?('CACHE')
ignored_entry = !bt.first || ignored.any?{ |s| bt.first.include?(s) }
unless cache_hit || ignored_entry
Rails.logger.debug("\e[35mCalled from:\e[0m #{bt.join("\n ")}")
end
end
ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
yield
end
end
end
end
# Wrap something in this block (for example, a piece of view)
with_query_trace do
# some code
end
# See backtraces from SQL calls in your log.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment