How to Ruby logo

How to remove select statements from Rails logger output

26 Aug 2022

In development the number of SELECT queries usually takes a lot of space of the total request output. Unless you’re specifically optimising those, it’s much cleaner to remove them. I’ve chosen to remove them in production as well, but feel free to adapt.

Take a peak at ActiveRecord::LogSubscriber to see what we’re overriding.

# config/initializers/silence_sql_select_logging.rb

module LoggerWithoutSelect
  def sql(event)
    super unless event.payload[:sql].present? && event.payload[:sql].include?("SELECT")
  end
end

ActiveRecord::LogSubscriber.prepend LoggerWithoutSelect
Rails.backtrace_cleaner.add_silencer { |line| line =~ /config\/initializers\/silence_sql_select_logging.rb:.*:in `sql'/ }

Well, that’s it, simple and effective! 😊