Module: LogStruct::Concerns::Configuration::ClassMethods

Extended by:
T::Sig
Included in:
LogStruct
Defined in:
lib/log_struct/concerns/configuration.rb

Constant Summary collapse

CONSOLE_COMMAND_ARGS =
T.let(["console", "c"].freeze, T::Array[String])
EMPTY_ARGV =
T.let([].freeze, T::Array[String])
RAILS_PATTERNS_LABEL =
"Rails filter_parameters"
RAILS_REGEXPS_LABEL =
"Rails filter_parameters (Regexps)"
RAILS_PATHS_LABEL =
"Rails filter_parameters (paths)"
RAILS_PATTERN_LABELS =
T.let([RAILS_PATTERNS_LABEL, RAILS_REGEXPS_LABEL].freeze, T::Array[String])
RAILS_FILTER_MASK =
"[FILTERED]"
RAILS_FILTER_PROBE =
"value"

Instance Method Summary collapse

Instance Method Details

#apply_initializer_settings! ⇒ void

This method returns an undefined value.

After config/initializers run: LOGSTRUCT_ENABLED takes precedence, then config.enabled if an initializer changed it, then config.enabled_environments if an initializer changed that



75
76
77
78
79
80
81
82
# File 'lib/log_struct/concerns/configuration.rb', line 75

def apply_initializer_settings!
  env_var_enabled = enabled_from_env_var
  if !env_var_enabled.nil?
    config.enabled = env_var_enabled
  elsif config.enabled == @enabled_from_rails_env && config.enabled_environments != @enabled_environments_from_rails_env
    set_enabled_from_rails_env!
  end
end

#config ⇒ LogStruct::Configuration



30
31
32
# File 'lib/log_struct/concerns/configuration.rb', line 30

def config
  LogStruct::Configuration.instance
end

#configuration ⇒ LogStruct::Configuration

(Can't use alias_method since this module is extended into LogStruct)



36
37
38
# File 'lib/log_struct/concerns/configuration.rb', line 36

def configuration
  config
end

#configuration=(config) ⇒ void

This method returns an undefined value.

Setter method to replace the configuration (for testing purposes)

Parameters:



42
43
44
# File 'lib/log_struct/concerns/configuration.rb', line 42

def configuration=(config)
  LogStruct::Configuration.set_instance(config)
end

#configure(&block) {|config| ... } ⇒ void

This method returns an undefined value.

Parameters:

Yields:



25
26
27
# File 'lib/log_struct/concerns/configuration.rb', line 25

def configure(&block)
  yield(config)
end

#enabled? ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/log_struct/concerns/configuration.rb', line 47

def enabled?
  config.enabled
end

#is_local? ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/log_struct/concerns/configuration.rb', line 85

def is_local?
  config.local_environments.include?(::Rails.env.to_sym)
end

#is_production? ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/log_struct/concerns/configuration.rb', line 90

def is_production?
  !is_local?
end

#merge_rails_filter_parameters! ⇒ void

This method returns an undefined value.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/log_struct/concerns/configuration.rb', line 95

def merge_rails_filter_parameters!
  return unless ::Rails.application.config.respond_to?(:filter_parameters)

  rails_filter_params = ::Rails.application.config.filter_parameters
  return unless rails_filter_params.is_a?(Array)
  return if rails_filter_params.empty?

  blocks, patterns = rails_filter_params.partition { |entry| callable_filter?(entry) }
  # Rails matches entries with a dot against a key's path (user.password), and the rest against the key
  path_patterns, key_patterns = patterns.partition { |entry| path_pattern?(entry) }
  regexps, partial_names = key_patterns.partition { |entry| entry.is_a?(Regexp) }
  # Rails gives blocks with any arity but 2 the parameters as a third argument
  path_blocks, key_blocks = blocks.partition { |block| arity_for_filter(block) == 3 }

  matchers = key_blocks.map { |block| build_callable_filter_matcher(block) }
  if partial_names.any?
    matchers << build_rails_pattern_matcher(partial_names, label: RAILS_PATTERNS_LABEL, matches_logstruct_fields: false)
  end
  if regexps.any?
    matchers << build_rails_pattern_matcher(regexps, label: RAILS_REGEXPS_LABEL, matches_logstruct_fields: true)
  end
  path_matchers = path_blocks.map { |block| build_path_block_matcher(block) }
  path_matchers << build_rails_path_matcher(path_patterns) if path_patterns.any?

  # Exact names also filter LogStruct's own fields, which the partial matches skip
  names = key_patterns.filter_map { |entry| entry.to_s.downcase.to_sym if entry.is_a?(Symbol) || entry.is_a?(String) }
  config.filters.filter_keys |= names

  # The latest list replaces the patterns merged before
  registered = config.filters.filter_matchers
  registered.reject! { |matcher| RAILS_PATTERN_LABELS.include?(matcher.label) }
  matchers.each do |matcher|
    registered << matcher unless registered.any? { |existing| existing.label == matcher.label }
  end
  registered_paths = config.filters.path_matchers
  registered_paths.reject! { |matcher| matcher.label == RAILS_PATHS_LABEL }
  path_matchers.each do |matcher|
    registered_paths << matcher unless registered_paths.any? { |existing| existing.label == matcher.label }
  end
end

#set_enabled_from_rails_env! ⇒ void

This method returns an undefined value.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/log_struct/concerns/configuration.rb', line 52

def set_enabled_from_rails_env!
  # 1. When LOGSTRUCT_ENABLED is set, it decides: on for true, t, yes, y or 1, off for anything else.
  # 2. Otherwise LogStruct is on when the Rails environment is in enabled_environments, the process
  #    isn't a console, and it's a server (server_process?) or runs in the test environment.
  env_var_enabled = enabled_from_env_var
  config.enabled = if env_var_enabled.nil?
    is_console = console_process?
    is_server = server_process?
    in_enabled_env = config.enabled_environments.include?(::Rails.env.to_sym)

    in_enabled_env && !is_console && (is_server || ::Rails.env.test?)
  else
    env_var_enabled
  end

  # apply_initializer_settings! compares the settings with these
  @enabled_from_rails_env = T.let(config.enabled, T.nilable(T::Boolean))
  @enabled_environments_from_rails_env = T.let(config.enabled_environments.dup, T.nilable(T::Array[Symbol]))
end