Class: DataRedactor::Integrations::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/data_redactor/integrations/logger.rb

Overview

Logger formatter that runs every log message through DataRedactor.redact before delegating to an inner formatter.

Examples:

Drop-in replacement for Ruby’s default formatter

logger = Logger.new($stdout)
logger.formatter = DataRedactor::Integrations::Logger.new
logger.info("Auth failed for user alice@example.com")
# => "I, [...] -- : Auth failed for user [REDACTED]"

Wrapping an existing formatter (e.g. Rails JSON logger)

logger.formatter = DataRedactor::Integrations::Logger.new(
  inner: Rails.logger.formatter,
  only:  [:credentials, :contact]
)

Instance Method Summary collapse

Constructor Details

#initialize(inner: ::Logger::Formatter.new, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • inner (#call, nil) (defaults to: ::Logger::Formatter.new)

    formatter to wrap. Defaults to Logger::Formatter.

  • only (Symbol, String, Array, nil) (defaults to: nil)

    forwarded to DataRedactor.redact.

  • except (Symbol, String, Array, nil) (defaults to: nil)

    forwarded to DataRedactor.redact.

  • placeholder (defaults to: DataRedactor::PLACEHOLDER_DEFAULT)

    forwarded to DataRedactor.redact.



25
26
27
28
29
30
# File 'lib/data_redactor/integrations/logger.rb', line 25

def initialize(inner: ::Logger::Formatter.new, only: nil, except: nil, placeholder: DataRedactor::PLACEHOLDER_DEFAULT)
  @inner = inner
  @only = only
  @except = except
  @placeholder = placeholder
end

Instance Method Details

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

Formatter contract — called by Logger for every emitted line. Lets the inner formatter render whatever it likes (string, exception, arbitrary object) and scrubs the resulting line in one pass. Keeps the exception cause chain intact so downstream formatters still see it.



36
37
38
39
# File 'lib/data_redactor/integrations/logger.rb', line 36

def call(severity, time, progname, msg)
  line = @inner.call(severity, time, progname, msg)
  DataRedactor.redact(line.to_s, only: @only, except: @except, placeholder: @placeholder)
end