Skip to content

Instantly share code, notes, and snippets.

@Yoshyn
Created November 15, 2016 13:28
Show Gist options
  • Save Yoshyn/dfa07ee41813f1b7a8ac2402f4fff4c2 to your computer and use it in GitHub Desktop.
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/)
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
@Yoshyn
Copy link
Author

Yoshyn commented Jun 20, 2017

Adding other services (Solr, redis, & other) : https://gist.github.com/mnutt/566725

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment