Class: TTY::Logger::DataFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/logger/data_filter.rb

Constant Summary collapse

FILTERED =
"[FILTERED]"
DOT =
"."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filters = [], mask: nil) ⇒ DataFilter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a data filter instance with filters.

Examples:

TTY::Logger::DataFilter.new(%w[foo], mask: "<SECRET>")

Parameters:

  • mask (String) (defaults to: nil)

    the mask to replace object with. Defaults to ‘“[FILTERED]”`



20
21
22
23
24
# File 'lib/tty/logger/data_filter.rb', line 20

def initialize(filters = [], mask: nil)
  @mask = mask || FILTERED
  @filters = filters
  @compiled_filters = compile(filters)
end

Instance Attribute Details

#compiled_filtersObject (readonly)

Returns the value of attribute compiled_filters.



9
10
11
# File 'lib/tty/logger/data_filter.rb', line 9

def compiled_filters
  @compiled_filters
end

#filtersObject (readonly)

Returns the value of attribute filters.



9
10
11
# File 'lib/tty/logger/data_filter.rb', line 9

def filters
  @filters
end

#maskObject (readonly)

Returns the value of attribute mask.



9
10
11
# File 'lib/tty/logger/data_filter.rb', line 9

def mask
  @mask
end

Instance Method Details

#filter(obj) ⇒ Object

Filter object for keys matching provided filters.

Examples:

data_filter = TTY::Logger::DataFilter.new(%w[foo])
data_filter.filter({"foo" => "bar"})
# => {"foo" => "[FILTERED]"}

Parameters:

  • obj (Object)

    the object to filter



37
38
39
40
41
42
43
# File 'lib/tty/logger/data_filter.rb', line 37

def filter(obj)
  return obj if filters.empty?

  obj.each_with_object({}) do |(k, v), acc|
    acc[k] = filter_val(k, v)
  end
end