Log4r::EmailOutputter

Attributes

server[R]
port[R]
domain[R]
acct[R]
authtype[R]
subject[R]
tls[R]

Public Class Methods

new(_name, hash={}) click to toggle source
    # File lib/log4r/outputter/emailoutputter.rb, line 12
12:     def initialize(_name, hash={})
13:       super(_name, hash)
14:       validate(hash)
15:       @buff = []
16:       begin 
17:         Logger.log_internal {
18:           "EmailOutputter '#{@name}' running SMTP client on #{@server}:#{@port}"
19:         }
20:       rescue Exception => e
21:         Logger.log_internal(2) {
22:           "EmailOutputter '#{@name}' failed to start SMTP client!"
23:         }
24:         Logger.log_internal {e}
25:         self.level = OFF
26:         raise e
27:       end
28:     end

Public Instance Methods

flush() click to toggle source

send out an email with the current buffer

    # File lib/log4r/outputter/emailoutputter.rb, line 31
31:     def flush
32:       synch { send_mail }
33:       Logger.log_internal {"Flushed EmailOutputter '#{@name}'"}
34:     end

Private Instance Methods

canonical_log(event) click to toggle source
    # File lib/log4r/outputter/emailoutputter.rb, line 78
78:     def canonical_log(event)
79:       synch {
80:         @buff.push case @formatfirst
81:           when true then @formatter.format event
82:           else event 
83:           end
84:         send_mail if @buff.size >= @buffsize or @immediate[event.level]
85:       }
86:     end
decode_immediate_at(hash) click to toggle source
    # File lib/log4r/outputter/emailoutputter.rb, line 45
45:     def decode_immediate_at(hash)
46:       @immediate = Hash.new
47:       _at = (hash[:immediate_at] or hash['immediate_at'])
48:       return if _at.nil?
49:       Log4rTools.comma_split(_at).each {|lname|
50:         level = LNAMES.index(lname)
51:         if level.nil?
52:           Logger.log_internal(2) do
53:             "EmailOutputter: skipping bad immediate_at level name '#{lname}'"
54:           end
55:           next
56:         end
57:         @immediate[level] = true
58:       }
59:     end
send_mail() click to toggle source
     # File lib/log4r/outputter/emailoutputter.rb, line 88
 88:     def send_mail
 89:       msg = 
 90:         case @formatfirst
 91:         when true then @buff.join 
 92:         else @buff.collect{|e| @formatter.format e}.join 
 93:         end
 94: 
 95:       ### build a mail header for RFC 822
 96:       rfc822msg =
 97:         "From: #{@from}\n" +
 98:         "To: #{@to}\n" +
 99:         "Subject: #{@subject}\n" +
100:         "Date: #{Time.now.strftime( "%a, %d %b %Y %H:%M:%S %z %Z")}\n" +
101:         "Message-Id: <#{"%.8f" % Time.now.to_f}@#{@domain}>\n\n" +
102:         "#{msg}"
103:   
104:       ### send email
105:       begin
106:   
107:         smtp = Net::SMTP.new( @server, @port )
108: 
109:         if ( @tls )
110: 
111:           # >1.8.7 has smtp_tls built in, 1.8.6 requires smtp_tls
112:           if RUBY_VERSION < "1.8.7" then
113:             begin
114:               require 'rubygems'
115:               require 'smtp_tls'
116:               smtp.enable_starttls if smtp.respond_to?(:enable_starttls)
117:             rescue LoadError => e
118:               Logger.log_internal(2) {
119:                 "EmailOutputter '#{@name}' unable to load smtp_tls, needed to support TLS on Ruby versions < 1.8.7"
120:               }
121:               raise e
122:             end
123:           else # RUBY_VERSION >= 1.8.7
124:             smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
125:           end
126: 
127:         end # if @tls
128: 
129:         smtp.start(@domain, @acct, @passwd, @authtype) do |s|
130:           s.send_message(rfc822msg, @from, @to)
131:         end
132:       rescue Exception => e
133:         Logger.log_internal(2) {
134:           "EmailOutputter '#{@name}' couldn't send email!"
135:         }
136:         Logger.log_internal {e}
137:         self.level = OFF
138:         raise e
139:       ensure @buff.clear
140:       end # begin
141:     end
validate(hash) click to toggle source
    # File lib/log4r/outputter/emailoutputter.rb, line 38
38:     def validate(hash)
39:       @buffsize = (hash[:buffsize] or hash['buffsize'] or 100).to_i
40:       @formatfirst = Log4rTools.decode_bool(hash, :formatfirst, false)
41:       decode_immediate_at(hash)
42:       validate_smtp_params(hash)
43:     end
validate_smtp_params(hash) click to toggle source
    # File lib/log4r/outputter/emailoutputter.rb, line 61
61:     def validate_smtp_params(hash)
62:       @from = (hash[:from] or hash['from'])
63:       raise ArgumentError, "Must specify from address" if @from.nil?
64:       _to = (hash[:to] or hash['to'] or "")
65:       @to = Log4rTools.comma_split(_to) 
66:       raise ArgumentError, "Must specify recepients" if @to.empty?
67:       @server = (hash[:server] or hash['server'] or 'localhost')
68:       @port = (hash[:port] or hash['port'] or 25).to_i
69:       @domain = (hash[:domain] or hash['domain'] or ENV['HOSTNAME'])
70:       @acct = (hash[:acct] or hash['acct'])
71:       @passwd = (hash[:passwd] or hash['passwd'])
72:       @authtype = (hash[:authtype] or hash['authtype'] or :cram_md5).to_s.to_sym
73:       @subject = (hash[:subject] or hash['subject'] or "Message of #{$0}")
74:       @tls = (hash[:tls] or hash['tls'] or nil)
75:       @params = [@server, @port, @domain, @acct, @passwd, @authtype]
76:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.