diff options
author | Olivier Lacan <hi@olivierlacan.com> | 2016-10-19 00:59:21 +0200 |
---|---|---|
committer | Olivier Lacan <hi@olivierlacan.com> | 2017-12-13 20:13:21 -0500 |
commit | 3876defd7c951335129fa92c573c7b8fce085aac (patch) | |
tree | ac53702d700c0f66f1bc235749f7ce1847395cc7 /activerecord/lib/active_record/log_subscriber.rb | |
parent | 65e994c063fdc42265712854598649ec7607faec (diff) | |
download | rails-3876defd7c951335129fa92c573c7b8fce085aac.tar.gz rails-3876defd7c951335129fa92c573c7b8fce085aac.tar.bz2 rails-3876defd7c951335129fa92c573c7b8fce085aac.zip |
Log call site for all queries
This new ActiveRecord configuration option allows you to easily
pinpoint what line of application code is triggering SQL queries in the
development log by appending below each SQL statement log the line of
Ruby code that triggered it.
It’s useful with N+1 issues, and to locate stray queries.
By default this new option ignores Rails and Ruby code in order to
surface only callers from your application Ruby code or your gems.
It is enabled on newly generated Rails 5.2 applications and can be
enabled on existing Rails applications:
```ruby
Rails.application.configure do
# ...
config.active_record.verbose_query_logs = true
end
```
The `rails app:upgrade` task will also add it to
`config/development.rb`.
This feature purposely avoids coupling with
ActiveSupport::BacktraceCleaner since ActiveRecord can be used without
ActiveRecord. This decision can be reverted in the future to allow more
configurable backtraces (the exclusion of gem callers for example).
Diffstat (limited to 'activerecord/lib/active_record/log_subscriber.rb')
-rw-r--r-- | activerecord/lib/active_record/log_subscriber.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb index 405f3a30c6..ba3419be6c 100644 --- a/activerecord/lib/active_record/log_subscriber.rb +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -90,6 +90,48 @@ module ActiveRecord def logger ActiveRecord::Base.logger end + + def debug(progname = nil, &block) + return unless super + + if ActiveRecord::Base.verbose_query_logs + log_query_source + end + end + + def log_query_source + source_line, line_number = extract_callstack(caller_locations) + + if source_line + if defined?(::Rails.root) + app_root = "#{::Rails.root.to_s}/".freeze + source_line = source_line.sub(app_root, "") + end + + logger.debug(" ↳ #{ source_line }:#{ line_number }") + end + end + + def extract_callstack(callstack) + line = callstack.find do |frame| + frame.absolute_path && !ignored_callstack(frame.absolute_path) + end + + offending_line = line || callstack.first + + [ + offending_line.path, + offending_line.lineno, + offending_line.label + ] + end + + RAILS_GEM_ROOT = File.expand_path("../../../..", __FILE__) + "/" + + def ignored_callstack(path) + path.start_with?(RAILS_GEM_ROOT) || + path.start_with?(RbConfig::CONFIG["rubylibdir"]) + end end end |