Skip to content

Instantly share code, notes, and snippets.

@leandro
Last active January 2, 2016 01:29
Show Gist options
  • Select an option

  • Save leandro/8231002 to your computer and use it in GitHub Desktop.

Select an option

Save leandro/8231002 to your computer and use it in GitHub Desktop.
module QueryTracer
# Borrowed some ANSI color sequences from elsewere
CLEAR = ActiveSupport::LogSubscriber::CLEAR
BOLD = ActiveSupport::LogSubscriber::BOLD
def self.start!
# Tap into notifications framework. Subscribe to sql.active_record messages
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
QueryTracer.publish(*args)
end
end
# Notice the 5 arguments that we can expect
def self.publish(name, started, ended, id, payload)
name = payload[:name]
sql = payload[:sql]
# Print out to logs
ActiveRecord::Base.logger.debug "#{BOLD} TRACE: #{sql}#{CLEAR}"
clean_trace.each do |line|
ActiveRecord::Base.logger.debug " #{line}"
end
end
def self.clean_trace
Rails.backtrace_cleaner.clean(caller[2..-1])
end
end
QueryTracer.start!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment