Class: RailsSemanticLogger::ActionMailer::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/rails_semantic_logger/action_mailer/log_subscriber.rb

Defined Under Namespace

Classes: EventFormatter

Instance Method Summary collapse

Instance Method Details

#deliver(event) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rails_semantic_logger/action_mailer/log_subscriber.rb', line 16

def deliver(event)
  # Rails gates this event with `subscribe_log_level :deliver, :debug`, so the upstream
  # subscriber only runs when the logger is at debug level (or lower). Match that here.
  return unless logger.debug?

  ex = event.payload[:exception_object]
  message_id = event.payload[:message_id]
  duration = event.duration.round(1)
  if ex
    log_with_formatter event: event, log_duration: true, level: :error do |_fmt|
      {
        message:   "Error delivering mail #{message_id} (#{duration}ms)",
        exception: ex
      }
    end
  else
    message =
      if event.payload[:perform_deliveries]
        "Delivered mail #{message_id} (#{duration}ms)"
      else
        "Skipped delivery of mail #{message_id} as `perform_deliveries` is false"
      end

    log_with_formatter event: event, log_duration: true do |_fmt|
      {message: message}
    end
  end
end

#process(event) ⇒ Object

An email was generated.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rails_semantic_logger/action_mailer/log_subscriber.rb', line 46

def process(event)
  # Rails gates this event with `subscribe_log_level :process, :debug` and emits the message
  # at debug level. Match both the gating and the level here.
  return unless logger.debug?

  mailer   = event.payload[:mailer]
  action   = event.payload[:action]
  duration = event.duration.round(1)
  log_with_formatter event: event, level: :debug do |_fmt|
    {message: "#{mailer}##{action}: processed outbound mail in #{duration}ms"}
  end
end