Module: Logstop

Defined in:
lib/logstop.rb,
lib/logstop/railtie.rb,
lib/logstop/version.rb,
lib/logstop/formatter.rb

Defined Under Namespace

Classes: Formatter, Railtie

Constant Summary collapse

FILTERED_STR =
"[FILTERED]".freeze
FILTERED_URL_STR =
"\\1[FILTERED]\\2".freeze
CREDIT_CARD_REGEX =
/\b[3456]\d{15}\b/
CREDIT_CARD_REGEX_DELIMITERS =
/\b[3456]\d{3}[\s+-]\d{4}[\s+-]\d{4}[\s+-]\d{4}\b/
EMAIL_REGEX =
/\b[\w]([\w+.-]|%2B)+(?:@|%40)[a-z\d-]+(?:\.[a-z\d-]+)*\.[a-z]+\b/i
IP_REGEX =
/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
PHONE_REGEX =
/\b(?:\+\d{1,2}\s)?\(?\d{3}\)?[\s+.-]\d{3}[\s+.-]\d{4}\b/
SSN_REGEX =
/\b\d{3}[\s+-]\d{2}[\s+-]\d{4}\b/
URL_PASSWORD_REGEX =
/((?:\/\/|%2F%2F)\S+(?::|%3A))\S+(@|%40)/
VERSION =
"0.2.8"

Class Method Summary collapse

Class Method Details

.guard(logger, **options) ⇒ Object



36
37
38
# File 'lib/logstop.rb', line 36

def self.guard(logger, **options)
  logger.formatter = Logstop::Formatter.new(logger.formatter, **options)
end

.scrub(msg, url_password: true, email: true, credit_card: true, phone: true, ssn: true, ip: false, scrubber: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logstop.rb', line 17

def self.scrub(msg, url_password: true, email: true, credit_card: true, phone: true, ssn: true, ip: false, scrubber: nil)
  msg = msg.to_s.dup

  # order filters are applied is important
  msg.gsub!(URL_PASSWORD_REGEX, FILTERED_URL_STR) if url_password
  msg.gsub!(EMAIL_REGEX, FILTERED_STR) if email
  if credit_card
    msg.gsub!(CREDIT_CARD_REGEX, FILTERED_STR)
    msg.gsub!(CREDIT_CARD_REGEX_DELIMITERS, FILTERED_STR)
  end
  msg.gsub!(PHONE_REGEX, FILTERED_STR) if phone
  msg.gsub!(SSN_REGEX, FILTERED_STR) if ssn
  msg.gsub!(IP_REGEX, FILTERED_STR) if ip

  msg = scrubber.call(msg) if scrubber

  msg
end