Class: ContextualLogger::Redactor

Inherits:
Object
  • Object
show all
Defined in:
lib/contextual_logger/redactor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRedactor

Returns a new instance of Redactor.



7
8
9
10
# File 'lib/contextual_logger/redactor.rb', line 7

def initialize
  @redaction_set   = Set.new
  @redaction_regex = nil
end

Instance Attribute Details

#redaction_regexObject (readonly)

Returns the value of attribute redaction_regex.



5
6
7
# File 'lib/contextual_logger/redactor.rb', line 5

def redaction_regex
  @redaction_regex
end

#redaction_setObject (readonly)

Returns the value of attribute redaction_set.



5
6
7
# File 'lib/contextual_logger/redactor.rb', line 5

def redaction_set
  @redaction_set
end

Instance Method Details

#redact(log_line) ⇒ String

Parameters:

  • log_line (String)

Returns:

  • (String)


26
27
28
29
30
31
32
# File 'lib/contextual_logger/redactor.rb', line 26

def redact(log_line)
  if redaction_regex
    log_line.gsub(redaction_regex, '<redacted>')
  else
    log_line
  end
end

#register_secret(sensitive_data) ⇒ Object



12
13
14
# File 'lib/contextual_logger/redactor.rb', line 12

def register_secret(sensitive_data)
  register_secret_regex(Regexp.escape(sensitive_data))
end

#register_secret_regex(regex) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/contextual_logger/redactor.rb', line 16

def register_secret_regex(regex)
  if redaction_set.add?(regex)
    @redaction_regex = Regexp.new(
      redaction_set.to_a.join('|')
    )
  end
end