Class: Logging::Appenders::Email

Inherits:
Logging::Appender show all
Includes:
Buffering
Defined in:
lib/logging/appenders/email.rb

Constant Summary

Constants included from Buffering

Buffering::DEFAULT_BUFFER_SIZE

Instance Attribute Summary collapse

Attributes included from Buffering

#auto_flushing, #buffer

Attributes inherited from Logging::Appender

#layout, #level, #name

Instance Method Summary collapse

Methods included from Buffering

#immediate_at=

Methods inherited from Logging::Appender

#<<, #append, #close, #closed?, #inspect

Constructor Details

#initialize(name, opts = {}) ⇒ Email

TODO: make the from/to fields modifiable

possibly the subject, too

Raises:

  • (ArgumentError)


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/logging/appenders/email.rb', line 20

def initialize( name, opts = {} )
  super(name, opts)

  af = opts.getopt(:buffsize) ||
       opts.getopt(:buffer_size) ||
       100
  configure_buffering({:auto_flushing => af}.merge(opts))

  # get the SMTP parameters
  @from = opts.getopt(:from)
  raise ArgumentError, 'Must specify from address' if @from.nil?

  @to = opts.getopt(:to, '').split(',')
  raise ArgumentError, 'Must specify recipients' if @to.empty?

  @server   = opts.getopt :server, 'localhost'
  @port     = opts.getopt :port, 25, :as => Integer
  @domain   = opts.getopt(:domain, ENV['HOSTNAME']) || 'localhost.localdomain'
  @acct     = opts.getopt :acct
  @passwd   = opts.getopt :passwd
  @authtype = opts.getopt :authtype, :cram_md5, :as => Symbol
  @subject  = opts.getopt :subject, "Message of #{$0}"
  @params   = [@server, @port, @domain, @acct, @passwd, @authtype]
end

Instance Attribute Details

#acctObject (readonly)

Returns the value of attribute acct.



15
16
17
# File 'lib/logging/appenders/email.rb', line 15

def acct
  @acct
end

#authtypeObject (readonly)

Returns the value of attribute authtype.



15
16
17
# File 'lib/logging/appenders/email.rb', line 15

def authtype
  @authtype
end

#domainObject (readonly)

Returns the value of attribute domain.



15
16
17
# File 'lib/logging/appenders/email.rb', line 15

def domain
  @domain
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/logging/appenders/email.rb', line 15

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



15
16
17
# File 'lib/logging/appenders/email.rb', line 15

def server
  @server
end

#subjectObject (readonly)

Returns the value of attribute subject.



15
16
17
# File 'lib/logging/appenders/email.rb', line 15

def subject
  @subject
end

Instance Method Details

#flushObject

call-seq:

flush

Create and send an email containing the current message buffer.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/logging/appenders/email.rb', line 50

def flush
  return self if buffer.empty?

  ### build a mail header for RFC 822
  rfc822msg =  "From: #{@from}\n"
  rfc822msg << "To: #{@to.join(",")}\n"
  rfc822msg << "Subject: #{@subject}\n"
  rfc822msg << "Date: #{Time.new.rfc822}\n"
  rfc822msg << "Message-Id: <#{"%.8f" % Time.now.to_f}@#{@domain}>\n\n"
  rfc822msg << buffer.join

  ### send email
  Net::SMTP.start(*@params) {|smtp| smtp.sendmail(rfc822msg, @from, @to)}
  self
rescue StandardError, TimeoutError => err
  self.level = :off
  ::Logging.log_internal {'e-mail notifications have been disabled'}
  ::Logging.log_internal(-2) {err}
ensure
  buffer.clear
end