Last active
January 2, 2016 01:29
-
-
Save leandro/8231002 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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