Class: DeprecatedAttributes::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/notifiers/rails_logger.rb

Overview

Log Subscriber

Instance Method Summary collapse

Instance Method Details

#deprecated_attributes(event) ⇒ Object



32
33
34
35
36
# File 'lib/notifiers/rails_logger.rb', line 32

def deprecated_attributes(event)
  deprecation_message = format_message(event.payload)

  log_method(deprecation_message, logger_config[:level])
end

#format_message(payload) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/notifiers/rails_logger.rb', line 20

def format_message(payload)
  title = log_color("DEPRECATION WARNING:", YELLOW)
  klass_str = log_color(payload[:klass], CYAN)
  attr_str = log_color(payload[:attribute], BLUE)
  args_str = log_color(payload[:args], MAGENTA)
  msg_str = payload[:msg] ? "#{log_color("DEPRECATION DETAILS:", YELLOW)} #{log_color(payload[:msg], MAGENTA)}" : ""

  backtrace = payload[:backtrace].join("\n")
  ["\n#{title} `#{attr_str}` is deprecated. Was called on #{klass_str} with args: #{args_str}\n#{msg_str}",
    backtrace].join("\n")
end

#log_color(text, color, bold: true) ⇒ Object



11
12
13
# File 'lib/notifiers/rails_logger.rb', line 11

def log_color(text, color, bold: true)
  logger_config[:color] ? color(text, color, bold) : text
end

#log_method(msg, level = :info) ⇒ Object



15
16
17
18
# File 'lib/notifiers/rails_logger.rb', line 15

def log_method(msg, level = :info)
  log_level = %i[debug info warn error fatal].include?(level) ? level : :unknown
  send(log_level, msg)
end

#loggerObject



38
39
40
# File 'lib/notifiers/rails_logger.rb', line 38

def logger
  ActiveRecord::Base.logger
end

#logger_configObject



7
8
9
# File 'lib/notifiers/rails_logger.rb', line 7

def logger_config
  DeprecatedAttributes.configuration.rails_logger || {}
end