Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable ActiveRecord logging for a certain column?

I'm running into a problem which, in my opinion, must be a problem for most rails users but I could not find any solution for it yet.

When, for instance, performing a file upload of a potentially large, binary file and storing it in the database, you most certainly don't want rails or ActiveRecord to log this specific field in development mode (log file, stdout). In case of a fairly big file, this causes the query execution to break and almost kills my terminal.

Is there any reliable and non-hacky method of disabling logging for particular fields? Remember, I'm not talking about disabling logging for request parameters - this has been solved quite nicely.

Thanks for any information on that!

like image 929
Remo Avatar asked Oct 24 '12 15:10

Remo


People also ask

Is there a way to disable logging for certain tables?

There are other RDBMS solutions that offer the ability to completely disable logging for certain tables. The data could be staged and aggregated in another system, and the results merged into the existing SQL Server database, which is well-protected by full logging and backups. Show activity on this post.

Is there a way to prevent logging in SQL Server?

The data could be staged and aggregated in another system, and the results merged into the existing SQL Server database, which is well-protected by full logging and backups. Show activity on this post. No, there is no way to prevent logging in SQL Server regardless of schema, table or database level.

How do I prevent users from logging in to a workstation?

Basically, you can use group policy to configure the workstation “Log on locally” setting, which controls who can log on interactively to a Windows machine, you could set the accounts "log on to," you could but these users in a gropu and then apply a deny locon locally.

Is there a way to disable logging of user operations?

Logging of user operations cannot be disabled. There is a class of operations called minimally-logged operations that only allows a transaction to be rolled back (as opposed to also being able to roll forward ).


2 Answers

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_truncate
end
like image 80
dbortz Avatar answered Sep 21 '22 18:09

dbortz


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, :trunkate
end

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

like image 35
Patrik Avatar answered Sep 18 '22 18:09

Patrik