I needed to filter some output when logging. Unfortunately, the filtering built into Rails was no help because the logging wasn’t coming from parameters. It also wasn’t coming from a Hash. (To filter a Hash, you can also use ActionDispatch::Http::ParameterFilter.) Instead, it was coming from a value inside another object.

Although this wasn’t my final solution, I still think it could be useful to someone:

# License: MIT, GPLv2
module InspectionHelper
  def self.filter(object)
    def object.ai(*)
      '[AWESOME-PRINT-FILTERED]'
    end

    def object.inspect
      '[INSPECT-FILTERED]'
    end
  end
end

This will filter output from both inspect and the equivalent in awesome_print (which was the pain point for me earlier today).

Usage:

data = []
InspectionHelper.filter(data)
data.inspect # => '[INSPECT-FILTERED]'

# Or nested objects:
h = { something_important: Object.new }
InspectionHelper.filter(h[:something_important])
h.inspect # => {:something_important=>[INSPECT-FILTERED]}