How to disable ActiveRecord logging for a certain column? How to disable ActiveRecord logging for a certain column? ruby-on-rails ruby-on-rails

How to disable ActiveRecord logging for a certain column?


If this helps anyone, here is a Rails 4.1 compatible version of the snippet above that also includes redaction of non-binary bind params (e.g. a text or json column), and increases the logging to 100 char before redaction. Thanks for everyone's help here!

class ActiveRecord::ConnectionAdapters::AbstractAdapter  protected  def log_with_binary_truncate(sql, name="SQL", binds=[], statement_name = nil, &block)    binds = binds.map do |col, data|      if data.is_a?(String) && data.size > 100        data = "#{data[0,10]} [REDACTED #{data.size - 20} bytes] #{data[-10,10]}"      end      [col, data]    end    sql = sql.gsub(/(?<='\\x[0-9a-f]{100})[0-9a-f]{100,}?(?=[0-9a-f]{100}')/) do |match|      "[REDACTED #{match.size} chars]"    end    log_without_binary_truncate(sql, name, binds, statement_name, &block)  end  alias_method_chain :log, :binary_truncateend


Create a file in config/initializers whitch modifies ActiveRecord::ConnectionAdapters::AbstractAdapter like so:

class ActiveRecord::ConnectionAdapters::AbstractAdapter   protected   def log_with_trunkate(sql, name="SQL", binds=[], &block)     b = binds.map {|k,v|       v = v.truncate(20) if v.is_a? String and v.size > 20       [k,v]     }     log_without_trunkate(sql, name, b, &block)   end   alias_method_chain :log, :trunkateend

This will trunkate all fields that are longer than 20 chars in the output log.


NOTE: Works with rails 3, but apparently not 4 (which was not released when this question was answered)

In your application.rb file:

config.filter_parameters << :parameter_name

This will remove that attribute from displaying in your logs, replacing it with [FILTERED]The common use case for filtering parameters is of course passwords, but I see no reason it shouldn't work with your binary file field.