Class: Harbor::LogAppenders::Email

Inherits:
Logging::Appender
  • Object
show all
Defined in:
lib/harbor/logging/appenders/email.rb

Defined Under Namespace

Classes: TrackedSubject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container, from, *addresses) ⇒ Email

Returns a new instance of Email.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/harbor/logging/appenders/email.rb', line 8

def initialize(container, from, *addresses)
  @container = container
  @from = from
  @addresses = addresses
  @tracked_subjects = []

  # Deliver emails with exact-match subjects only every 10 Minutes
  @duplicate_subject_delivery_threshold = 60 * 10

  super("exception_notifier", :level => :error)
end

Instance Attribute Details

#duplicate_subject_delivery_thresholdObject

Returns the value of attribute duplicate_subject_delivery_threshold.



6
7
8
# File 'lib/harbor/logging/appenders/email.rb', line 6

def duplicate_subject_delivery_threshold
  @duplicate_subject_delivery_threshold
end

Instance Method Details

#write(event) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/harbor/logging/appenders/email.rb', line 20

def write(event)
  unless @level > event.level
    flush_expired_subjects

    data = event.data
    subject = data.split($/, 2)[0]

    if tracked_subject = @tracked_subjects.detect { |tracked_subject| tracked_subject.subject == subject }
      tracked_subject.occurances << Time.now
      
      # Don't send the email if we've already sent one within the time threshold
      return false if (Time.now - tracked_subject.sent_at) < @duplicate_subject_delivery_threshold
    else
      tracked_subject = TrackedSubject.new(subject)
      tracked_subject.occurances << Time.now

      @tracked_subjects << tracked_subject
    end

    mailer = @container.get(:mailer)
    mailer.from = @from
    mailer.to = @addresses

    data << "\n(from: #{`hostname`.chomp}, PID: #{Process.pid})"

    mailer.subject = "[ERROR] #{subject}"
    mailer["x_priority"] = "1 (Highest)"
    mailer["x_msmail_priority"] = "High"
    mailer.text = if tracked_subject.occurances.size > 1
      "Repeated #{tracked_subject.occurances.size} times since #{tracked_subject.sent_at.strftime('%Y-%m-%d %R%z')}\n\n#{data}"
    else
      data
    end
    mailer.send!

    tracked_subject.occurances.clear
    tracked_subject.sent_at = Time.now
  end
end