Posted about 1 month ago. Visible to the public.

How to fix: Rails query logs always show lib/active_record/log_subscriber.rb as source

Rails 5.2+ supports "verbose query logs" where it shows the source of a query in the application log.
Normally, it looks like this:

Copy
User Load (0.5ms) SELECT "users".* FROM "users" WHERE ... ↳ app/controllers/users_controller.rb:42:in `load_users'

However, you may encounter ActiveRecord's LogSubscriber as the source for all/most queries which is not helpful at all:

Copy
User Load (0.5ms) SELECT "users".* FROM "users" WHERE ... ↳ activerecord (6.0.3.3) lib/active_record/log_subscriber.rb:100:in `debug'

While there was an issue back in 2018 which can be resolved by upgrading Rails, this also happens when ActiveSupport's BacktraceCleaner is configured to not hide gems from the backtrace. In such cases, log_subscriber.rb is the inner-most (i.e. "best") source in the backtrace.

You probably have an initializer with this line:

Copy
Rails.backtrace_cleaner.remove_silencers!

Remove it, and your verbose query logs will be helpful.

If you're using verbose query logs only in development, you may also choose to enable silencers in development only:

Copy
unless Rails.configuration.active_record.verbose_query_logs Rails.backtrace_cleaner.remove_silencers! end

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
about 1 month ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more