Class: Logging::Appenders::Email

Inherits:
Logging::Appender show all
Defined in:
lib/gems/logging-0.9.4/lib/logging/appenders/email.rb

Instance Attribute Summary collapse

Attributes inherited from Logging::Appender

#layout, #level, #name

Instance Method Summary collapse

Methods inherited from Logging::Appender

#<<, [], []=, #append, #closed?, #inspect, remove, stderr, stdout

Constructor Details

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

Returns a new instance of Email.

Raises:

  • (ArgumentError)


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
44
45
46
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 16

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

  @buff = []
  @buffsize = opts.getopt :buffsize, 100, :as => Integer

  # get the immediate levels -- no buffering occurs at these levels, and
  # an e-mail is sent as soon as possible
  @immediate = []
  opts.getopt(:immediate_at, '').split(',').each do |lvl|
    num = ::Logging.level_num(lvl.strip)
    next if num.nil?
    @immediate[num] = true
  end

  # 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.



14
15
16
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 14

def acct
  @acct
end

#authtypeObject (readonly)

Returns the value of attribute authtype.



14
15
16
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 14

def authtype
  @authtype
end

#domainObject (readonly)

Returns the value of attribute domain.



14
15
16
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 14

def domain
  @domain
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 14

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



14
15
16
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 14

def server
  @server
end

#subjectObject (readonly)

Returns the value of attribute subject.



14
15
16
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 14

def subject
  @subject
end

Instance Method Details

#close(footer = true) ⇒ Object

call-seq:

close( footer = true )

Close the e-mail appender and then flush the message buffer. This will ensure that a final e-mail is sent with any remaining messages.



64
65
66
67
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 64

def close( footer = true )
  super
  flush
end

#flushObject

call-seq:

flush

Create and send an email containing the current message buffer.



53
54
55
56
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 53

def flush
  sync { send_mail }
  self
end

#queued_messagesObject

cal-seq:

queued_messages    => integer

Returns the number of messages in the buffer.



74
75
76
# File 'lib/gems/logging-0.9.4/lib/logging/appenders/email.rb', line 74

def queued_messages
  @buff.length
end