In order to debug a problem, which only occurred in production, we recently wanted to tweak our Rails SQL logs to only show the access to a specific table.
Here's what we did to accomplish this. We created a file
initializers/filter_sql_log.rb with this content:
alias :old_sql :sql
if event.payload[:sql].include? 'users'
This monkey-patches the
ActiveRecord::LogSubscriber class and only delegates to the old logging method, if the SQL statement includes the string
By default, SQL logging is deactivated in the Rails production environment. Therefore we needed to change
config/environments/production.rb like this:
config.log_level = :debug