Skip to content
Rails plugin that logs/displays a backtrace of all SQL queries executed by Active Record
Branch: master
Clone or download
brunofacca Merge pull request #42 from brunofacca/improve-backtrace-cleaner-sile…

Improve backtrace cleaner silencers for :app level
Latest commit 7a7241c Mar 12, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Improve backtrace cleaner silencers for :app level Mar 12, 2019
spec Add .suppress_logging_of_db_reads option and improve README Mar 5, 2019
Gemfile Add Rails 4 to the CI and specify dependency versions in gemspec Mar 10, 2019 Improve backtrace cleaner silencers for :app level Mar 12, 2019
MIT-LICENSE Update README Mar 11, 2019

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.


  1. Add the following to your Gemfile:

    group :development do
      gem 'active_record_query_trace'
  2. 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
  3. Restart the Rails development server.


Backtrace level

There are three levels of debug.

  • :app - includes only application trace lines (files in the Rails.root directory);
  • :rails - includes all trace lines except the ones from the application (all files except those in Rails.root).
  • :full - full backtrace (includes all files), useful for debugging gems.
ActiveRecordQueryTrace.level = :app # default

Display the trace only for read or write queries

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

Suppress DB read queries

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

Ignore cached queries

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.

Limit the number of lines in the backtrace

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.

Colorize the backtrace

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


Bug reports

Please use the issue tracker to report any bugs.

Test environment

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.


  1. Create an issue and describe your idea
  2. Fork it
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Implement your changes;
  5. Run the test suite (rspec)
  6. Commit your changes (git commit -m 'Add some feature')
  7. Publish the branch (git push origin my-new-feature)
  8. Create a Pull Request


Released under the MIT License.

You can’t perform that action at this time.