Module: GetaroundUtils::Mixins::Loggable

Included in:
Utils::HandleError
Defined in:
lib/getaround_utils/mixins/loggable.rb

Constant Summary collapse

MONITORABLE_LOG_PREFIX =
"monitorable_log__"

Instance Method Summary collapse

Instance Method Details

#base_append_infos_to_loggable(payload) ⇒ Object



13
14
15
16
17
18
# File 'lib/getaround_utils/mixins/loggable.rb', line 13

def base_append_infos_to_loggable(payload)
  payload[:origin] = loggable_class_name
  return unless respond_to?(:append_infos_to_loggable)

  append_infos_to_loggable(payload)
end

#loggable_class_nameObject



9
10
11
# File 'lib/getaround_utils/mixins/loggable.rb', line 9

def loggable_class_name
  @loggable_class_name ||= is_a?(Class) ? name : self.class.name
end

#loggable_log(severity, message, payload = {}) ⇒ Object



28
29
30
31
# File 'lib/getaround_utils/mixins/loggable.rb', line 28

def loggable_log(severity, message, payload = {})
  base_append_infos_to_loggable(payload)
  loggable_logger.send(severity.to_sym, msg: message, **payload)
end

#loggable_loggerObject



24
25
26
# File 'lib/getaround_utils/mixins/loggable.rb', line 24

def loggable_logger
  (logger if respond_to?(:logger)) || (Rails.logger if defined?(Rails)) || loggable_logger_fallback
end

#loggable_logger_fallbackObject



20
21
22
# File 'lib/getaround_utils/mixins/loggable.rb', line 20

def loggable_logger_fallback
  @loggable_logger_fallback ||= Logger.new($stdout)
end

#monitorable_log(event_name, **options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/getaround_utils/mixins/loggable.rb', line 35

def monitorable_log(event_name, **options)
  monitorable_threshold = monitorable_threshold(event_name)
  return if monitorable_threshold.blank?

  loggable_log(
    :info,
    monitorable_log_message(event_name),
    alert_threshold: monitorable_threshold,
    **options
  )
end

#monitorable_log_message(event_name) ⇒ Object



55
56
57
# File 'lib/getaround_utils/mixins/loggable.rb', line 55

def monitorable_log_message(event_name)
  MONITORABLE_LOG_PREFIX + event_name
end

#monitorable_threshold(event_name) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/getaround_utils/mixins/loggable.rb', line 47

def monitorable_threshold(event_name)
  monitorable_threshold = ENV["#{monitorable_log_message(event_name)}_threshold".upcase]&.to_i
  if monitorable_threshold.nil? && monitorable_rails_config_defined?
    monitorable_threshold = Rails.application.config.monitorable_log_thresholds&.dig(event_name.to_sym)
  end
  monitorable_threshold
end