Class: Scrubber::LogFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/scrubber/log_formatter.rb,
sig/scrubber_rb.rbs

Overview

Wraps a Logger formatter and redacts its output.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter = nil, scrubber: nil, **options) ⇒ LogFormatter

Returns a new instance of LogFormatter.

Parameters:

  • formatter (#call) (defaults to: nil)

    the formatter to wrap. Defaults to Logger::Formatter, which is what a bare Logger uses.

  • scrubber (Scrubber::Instance) (defaults to: nil)

    a prebuilt engine, if you have one.

  • options (Hash)

    otherwise, options for Scrubber.new.

  • scrubber: (Instance, nil) (defaults to: nil)
  • (Object)


23
24
25
26
# File 'lib/scrubber/log_formatter.rb', line 23

def initialize(formatter = nil, scrubber: nil, **options)
  @formatter = formatter || self.class.default_formatter
  @scrubber = scrubber || Scrubber.new(**options)
end

Instance Attribute Details

#scrubberInstance (readonly)

Returns the value of attribute scrubber.

Returns:



17
18
19
# File 'lib/scrubber/log_formatter.rb', line 17

def scrubber
  @scrubber
end

Class Method Details

.default_formatterObject

Returns:

  • (Object)


35
36
37
38
# File 'lib/scrubber/log_formatter.rb', line 35

def self.default_formatter
  require "logger"
  ::Logger::Formatter.new
end

Instance Method Details

#call(severity, time, progname, msg) ⇒ Object

Parameters:

  • severity (String)
  • time (Time)
  • progname (Object)
  • msg (Object)

Returns:

  • (Object)


28
29
30
31
32
33
# File 'lib/scrubber/log_formatter.rb', line 28

def call(severity, time, progname, msg)
  formatted = @formatter.call(severity, time, progname, msg)
  return formatted unless formatted.is_a?(String)

  @scrubber.scrub(formatted)
end