How to disable ActiveRecord logging for a certain column?

rails logger
rails server logs
rails log settings
how to add log in rails
rails logger flush
rails log formatter
rails log request
rails production log

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!

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

I'm trying to debug something and using "puts" to print out some relevant data. However, the sql query output is making it hard to read. Edit: I  Per answer cited in comments, you need to add the following line to turn off SQL logging: ActiveRecord::Base.logger = nil. According to your posted config, you have ActiveRecord::Base.logger.level = Logger::INFO instead. Replace this line with the one above and SQL logging will be disabled.

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.

Is there any reliable and non-hacky method of disabling logging for particular fields? Out of the box Rails 5 truncates binary data, but not long text columns. 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. share. Share a link to this answer.

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.

Removing the email field Since you are storing the e-mail address along with 180 RAILS SOLUTIONS: RAILS MADE EASY Removing the email field Logging  You can set the log level in your environment file to debug so that you can get maximum data out of your application and can use the same for tuning. config.log_level = :debug To log it to STDOUT, you can add the following snippet to your specific environment file. ActiveRecord::Base.logger = Logger.new(STDOUT) References: Rails Guide about debugging

Here's an implementation of the approach suggested by @Patrik that works for both inserts and updates against PostgreSQL. The regex may need to be tweaked depending upon the formatting of the SQL for other databases.

class ActiveRecord::ConnectionAdapters::AbstractAdapter
   protected

   def log_with_binary_truncate(sql, name="SQL", binds=[], &block)
    binds = binds.map do |col, data|
      if col.type == :binary && data.is_a?(String) && data.size > 27
        data = "#{data[0,10]}[REDACTED #{data.size - 20} bytes]#{data[-10,10]}"
      end
      [col, data]
    end

    sql = sql.gsub(/(?<='\\x[0-9a-f]{20})[0-9a-f]{20,}?(?=[0-9a-f]{20}')/) do |match|
      "[REDACTED #{match.size} chars]"
    end

    log_without_binary_truncate(sql, name, binds, &block)
   end

   alias_method_chain :log, :binary_truncate
end

I'm not deliriously happy with it, but it's good enough for now. It preserves the first and last 10 bytes of the binary string and indicates how many bytes/chars were removed out of the middle. It doesn't redact unless the redacted text is longer than the replacing text (i.e. if there aren't at least 20 chars to remove, then "[REDACTED xx chars]" would be longer than the replaced text, so there's no point). I did not do performance testing to determine whether using greedy or lazy repetition for the redacted chunk was faster. My instinct was to go lazy, so I did, but it's possible that greedy would be faster especially if there is only one binary field in the SQL.

Rails: Disabling logging entirely. In an environment: Copy. config.logger = Logger.new('/dev/null'). Protect your Rails app from security breaches. Does your version of Ruby on Rails still receive security updates? log.info 'Some information' log.debug 'Debugging hints' log.error Rails: Disable options of a select field. Freeze rows or columns. Freeze the first column. Select View > Freeze Panes > Freeze First Column. The faint line that appears between Column A and B shows that the first column is frozen. Freeze the first two columns. Select the third column.

In rails 5 you could put it in initializer:

module SqlLogFilter

  FILTERS = Set.new(%w(geo_data value timeline))
  def render_bind(attribute)
    return [attribute.name, '<filtered>'] if FILTERS.include?(attribute.name)
    super
  end

end
ActiveRecord::LogSubscriber.prepend SqlLogFilter

For filter attributes geo_data, value and timeline for instance.

The configuration file config/application.rb and environment-specific configuration files Setting config.assets.logger to false will turn off served assets logging. By default, Rails assumes that primary key columns are named id (and this  Click in the Impersonation Box and start typing Replace and press enter (See Attachment 1) 5. Click on these permissions. 6. Select Add from the Replace list item permissions pop up (See Attachment 1) 7. Select Choose in the Add Permissions pop up and add the users or groups you would like to change permissions for.

To retrieve objects from the database, Active Record provides several finder If you would like to only grab a single record per unique value in a certain field, you Automatic EXPLAIN gets disabled if Active Record has no logger, regardless  Ideally, it would be great if I can just disable it and re-enable it with a command in the console. I'm trying to debug something and using "puts" to print out some relevant data. However, the sql query output is making it hard to read.

Rails will use that particular setting to configure Active Record. config.​disable_sandbox controls whether or not someone can start a console in sandbox It also filters out sensitive values of database columns when call #​inspect on an Active Record object. config.log_formatter defines the formatter of the Rails logger. Disable Rails SQL logging in console (5) Is there a way to disable SQL query logging when I'm executing commands in the console? Ideally, it would be great if I can just disable it and re-enable it with a command in the console.

Attributes do not need to be backed by a database column. The type of an attribute is given the opportunity to change how dirty tracking is performed. Added bin/rails dev:cache to enable or disable caching in development mode. Generate applications with an option to log to STDOUT in production using the  In case someone wants to actually get SQL query logging (without changing the logging level and when logging from their AR models): . The line that writes to the log (in Rails 3.2.16, anyway) is a call to "debug" in lib / active_record / log_subscriber.rb: 50.

Comments
  • There is an option to filter that like passwords, do not know if that would help. Would you consider overriding ActiveRecord execute hacky? if not then there's your hint.
  • This definitely works up to Rails 4.2 but doesn't work in 5.0+. As best I can tell, it's not needed in Rails 5 either because Rails seems to have similar functionality built in.
  • @Ritchie what is built in functionality?
  • Should also note that this will only truncate insert requests. in the event of update everything is thrown in in the sql param so that also needs to be truncated.
  • wrong number of arguments (3 for 2) in the 'log_without_trunkate' call in Rails 3.0.9.
  • The log method for 3.0.x rails use a log method that only takes two arguments 'sql' and 'name'. so remove binds and the block from argument and just filter out from the sql argument.
  • wrong number of arguments (4 for 1..3) in the 'log_without_trunkate' call in Rails 4.1.4
  • @Colin see dbortz solution below for rails 4.1
  • Doesn't this just filter the HTTP request parameters rather than the parameters in the SQL log?
  • Nope, it filters everything in the rails logs. Both the params hash on incoming requests and SQL statements logged will show [FILTERED]