Created
November 15, 2016 13:28
-
-
Save Yoshyn/dfa07ee41813f1b7a8ac2402f4fff4c2 to your computer and use it in GitHub Desktop.
Give the backtrace for all the SQL query performed in development mode. (into config/initializers/)
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
unless Rails.env.production? | |
module LogQuerySources | |
LOG_QUERY_LINES = ENV.fetch('LOG_QUERY_LINES', 3).to_i | |
def debug(*args, &block) | |
return unless super | |
backtrace = Rails.backtrace_cleaner.clean(caller) | |
backtrace.take(LOG_QUERY_LINES).each do |caller_line| | |
next if caller_line.include?('/initializers/active_record_log_subscriber/') | |
logger.debug(" ↳ #{ caller_line.sub("#{ Rails.root }/", '') }") | |
end | |
if (missing_row_count = (backtrace.count - LOG_QUERY_LINES)) > 0 | |
if missing_row_count > 1 | |
logger.debug(" ↳ ... (There's #{missing_row_count} more rows available. Please change LOG_QUERY_LINES if you want more.)") | |
end | |
logger.debug(" ↳ #{ backtrace.last.sub("#{ Rails.root }/", '') }") | |
end | |
end | |
end | |
ActiveRecord::LogSubscriber.send :prepend, LogQuerySources | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding other services (Solr, redis, & other) : https://gist.github.com/mnutt/566725