Created
August 8, 2012 05:58
-
-
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
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
| 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 |
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
| # 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