Class: DataRedactor::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/data_redactor/railtie.rb

Overview

Rails wiring for data_redactor. Requiring this file installs a Railtie that wraps the Rails logger's formatter and appends a filter_parameters entry, so an app gets redaction without writing either integration by hand.

Rails is never a runtime dependency of the gem: this file is only loaded when the application requires it, following the same opt-in pattern as every other integration.

Examples:

Enable everything with the defaults

# Gemfile
gem "data_redactor", require: "data_redactor/railtie"

Tune it from an initializer

# config/initializers/data_redactor.rb
Rails.application.configure do
  config.data_redactor.only        = [:credentials, :financial]
  config.data_redactor.placeholder = :tagged
  config.data_redactor.logger      = false   # leave the logger alone
end

Class Method Summary collapse

Class Method Details

.redaction_options(config) ⇒ Hash

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.

only/except are read with [] rather than the OrderedOptions reader: only and except are Enumerable methods, so the reader would return a Method-backed result instead of nil when the app never set them.

Parameters:

  • config (ActiveSupport::OrderedOptions)

    the config.data_redactor options

Returns:



41
42
43
44
45
46
47
# File 'lib/data_redactor/railtie.rb', line 41

def self.redaction_options(config)
  {
    only: config[:only],
    except: config[:except],
    placeholder: config.placeholder
  }
end

.wrap_logger(logger, options) ⇒ void

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.

This method returns an undefined value.

A BroadcastLogger writes to each of its sinks directly, so a formatter set on the broadcast itself never runs — each sink has to be wrapped instead.

Parameters:

  • logger (#formatter, ActiveSupport::BroadcastLogger, nil)

    logger to wrap

  • options (Hash)

    kwargs forwarded to Integrations::Logger



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/data_redactor/railtie.rb', line 75

def self.wrap_logger(logger, options)
  return if logger.nil?

  if logger.respond_to?(:broadcasts)
    logger.broadcasts.each { |sink| wrap_logger(sink, options) }
    return
  end

  return unless logger.respond_to?(:formatter) && logger.respond_to?(:formatter=)

  inner = logger.formatter || ::Logger::Formatter.new
  return if inner.is_a?(Integrations::Logger)

  logger.formatter = Integrations::Logger.new(inner: inner, **options)
end