Displays a backtrace for each query in Rails' development console and log. Allows you to track down where queries are executed in your application. Useful for performance optimizations and for finding where to start when making changes to a large application.
When enabled, every query will be logged like:
D, [2019-03-03T19:50:41.061115 #25560] DEBUG -- : User Load (0.1ms) SELECT "users".* FROM "users"
D, [2019-03-03T19:50:41.062492 #25560] DEBUG -- : Query Trace:
app/models/concerns/is_active.rb:11:in `active?'
app/models/user.rb:67:in `active?'
app/decorators/concerns/status_methods.rb:42:in `colored_status'
app/views/shared/companies/_user.html.slim:28:in `block in _app_views_users_html_slim___2427456029761612502_70304705622200'
app/views/shared/companies/_user.html.slim:27:in `_app_views_users_html_slim___2427456029761612502_70304705622200'
- Ruby >= 2.3;
- Rails 4.2, 5.2, or 6.
-
Add the following to your Gemfile:
group :development do gem 'active_record_query_trace' end
-
Create an initializer such as
config/initializers/active_record_query_trace.rb
to enable the gem. If you want to customize how the gem behaves, you can add any combination of the following options to the initializer as well.if Rails.env.development? ActiveRecordQueryTrace.enabled = true # Optional: other gem config options go here end
-
Restart the Rails development server.
There are three levels of debug.
:app
- includes only application trace lines (files in theRails.root
directory);:rails
- includes all trace lines except the ones from the application (all files except those inRails.root
).:full
- full backtrace (includes all files), useful for debugging gems.
ActiveRecordQueryTrace.level = :app # default
You can choose to display the backtrace only for DB reads, writes or both.
:all
- display backtrace for all queries;:read
- display backtrace only for DB read operations (SELECT);:write
- display the backtrace only for DB write operations (INSERT, UPDATE, DELETE).
ActiveRecordQueryTrace.query_type = :all # default
If set to true
, this option will suppress all log lines generated by DB
read (SELECT) operations, leaving only the lines generated by DB write queries
(INSERT, UPDATE, DELETE). Beware, the entire log line is suppressed, not only
the backtrace. Useful to reduce noise in the logs (e.g., N+1 queries) when you
only care about queries that write to the DB.
ActiveRecordQueryTrace.suppress_logging_of_db_reads = false # default
By default, a backtrace will be logged for every query, even cached queries that do not actually hit the database. You might find it useful not to print the backtrace for cached queries:
ActiveRecordQueryTrace.ignore_cached_queries = true # Default is false.
If you are working with a large app, you may wish to limit the number of lines displayed for each query.
ActiveRecordQueryTrace.lines = 10 # Default is 5. Setting to 0 includes entire trace.
To colorize the output:
ActiveRecordQueryTrace.colorize = false # No colorization (default)
ActiveRecordQueryTrace.colorize = :light_purple # Colorize in specific color
ActiveRecordQueryTrace.colorize = true # Colorize in default color
Valid colors are: :black
, :red
, :green
, :brown
, :blue
, :purple
, :cyan
,
:gray
, :dark_gray
, :light_red
, :light_green
, :yellow
, :light_blue
,
:light_purple
, :light_cyan
, :white
.
- Cody Caughlan - Original author.
- Bruno Facca - Current maintainer. LinkedIn
Please use the issue tracker to report any bugs.
This gem uses RSpec for testing. You can run the test suite by executing the
rspec
command. It has a decent test coverage and the test suite takes less than
a second to run as it uses an in-memory SQLite DB.
- Create an issue and describe your idea
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Implement your changes;
- Run the test suite (
rspec
) - Commit your changes (
git commit -m 'Add some feature'
) - Publish the branch (
git push origin my-new-feature
) - Create a Pull Request
Released under the MIT License.