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])
CI_FALSE_VALUES =
T.let(["false", "0", "no"].freeze, T::Array[String])

Instance Method Summary collapse

Instance Method Details

#configObject



23
24
25
# File 'lib/log_struct/concerns/configuration.rb', line 23

def config
  LogStruct::Configuration.instance
end

#configurationObject



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

def configuration
  config
end

#configuration=(config) ⇒ Object



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

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

#configure {|config| ... } ⇒ Object

Yields:



18
19
20
# File 'lib/log_struct/concerns/configuration.rb', line 18

def configure(&block)
  yield(config)
end

#enabled?Boolean

Returns:



40
41
42
# File 'lib/log_struct/concerns/configuration.rb', line 40

def enabled?
  config.enabled
end

#is_local?Boolean

Returns:



70
71
72
# File 'lib/log_struct/concerns/configuration.rb', line 70

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

#is_production?Boolean

Returns:



75
76
77
# File 'lib/log_struct/concerns/configuration.rb', line 75

def is_production?
  !is_local?
end

#merge_rails_filter_parameters!Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
# File 'lib/log_struct/concerns/configuration.rb', line 80

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?

  symbol_filters = T.let([], T::Array[Symbol])
  matchers = T.let([], T::Array[ConfigStruct::FilterMatcher])
  leftovers = T.let([], T::Array[T.untyped])

  rails_filter_params.each do |entry|
    matcher = build_filter_matcher(entry)

    if matcher
      matchers << matcher
      next
    end

    normalized_symbol = normalize_filter_symbol(entry)
    if normalized_symbol
      symbol_filters << normalized_symbol
    else
      leftovers << entry
    end
  end

  if symbol_filters.any?
    config.filters.filter_keys |= symbol_filters
  end

  if matchers.any?
    matchers.each do |matcher|
      existing = config.filters.filter_matchers.any? do |registered|
        registered.label == matcher.label
      end
      config.filters.filter_matchers << matcher unless existing
    end
  end

  replace_filter_parameters(rails_filter_params, leftovers)
end

#set_enabled_from_rails_env!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/log_struct/concerns/configuration.rb', line 45

def set_enabled_from_rails_env!
  # Set enabled based on current Rails environment and the LOGSTRUCT_ENABLED env var.
  # Precedence:
  # 1. Check if LOGSTRUCT_ENABLED env var is defined (not an empty string)
  #    - Sets enabled=true only when value is "true", "yes", "1", etc.
  #    - Sets enabled=false when value is any other value
  # 2. Otherwise, check if current Rails environment is in enabled_environments
  #    AND one of: Rails::Server is defined, OR test environment with CI=true
  #    BUT NOT Rails::Console (to exclude interactive console)
  # 3. Otherwise, leave as config.enabled (defaults to true)

  # Then check if LOGSTRUCT_ENABLED env var is set
  config.enabled = if ENV["LOGSTRUCT_ENABLED"]
    %w[true t yes y 1].include?(ENV["LOGSTRUCT_ENABLED"]&.strip&.downcase)
  else
    is_console = console_process?
    is_server = server_process?
    ci_build?
    in_enabled_env = config.enabled_environments.include?(::Rails.env.to_sym)

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