Parent

Included Modules

Logging::Appenders::Email

Provides an appender that can send log messages via email to a list of recipients.

Attributes

authentication[R]
to[R]
port[R]
address[RW]
domain[RW]
from[RW]
subject[RW]
user_name[RW]
password[RW]
enable_starttls_auto[RW]

Public Class Methods

com', :subject => 'Whoops!' ) click to toggle source

Create a new email appender that will buffer messages and then send them out in batches to the listed recipients. See the options below to configure how emails are sent through you mail server of choice. All the buffering options apply to the email appender.

The following options are required:

 :from - The base filename to use when constructing new log
         filenames.
 :to   - The list of email recipients either as an Array or a comma
         separated list.

The following options are optional:

 :subject   - The subject line for the email.
 :address   - Allows you to use a remote mail server. Just change it
              from its default "localhost" setting.
 :port      - On the off chance that your mail server doesn't run on
              port 25, you can change it.
 :domain    - If you need to specify a HELO domain, you can do it here.
 :user_name - If your mail server requires authentication, set the user
              name in this setting.
 :password  - If your mail server requires authentication, set the
              password in this setting.
 :authentication - If your mail server requires authentication, you need
                   to specify the authentication type here. This is a
                   symbol and one of :plain (will send the password in
                   the clear), :login (will send password Base64
                   encoded) or :cram_md5 (combines a Challenge/Response
                   mechanism to exchange information and a cryptographic
                   Message Digest 5 algorithm to hash important
                   information)
 :enable_starttls_auto - When set to true, detects if STARTTLS is
                         enabled in your SMTP server and starts to use it.

Example:

Setup an email appender that will buffer messages for up to 1 minute, and only send messages for ERROR and FATAL messages. This example uses Google’s SMTP server with authentication to send out messages.

  Logger.appenders.email( 'email',
      :from       => "server@example.com",
      :to         => "developers@example.com",
      :subject    => "Application Error [#{%x(uname -n).strip}]",

      :address    => "smtp.google.com",
      :port       => 443,
      :domain     => "google.com",
      :user_name  => "example",
      :password   => "12345",
      :authentication => :plain,
      :enable_starttls_auto => true,

      :auto_flushing => 200,     # send an email after 200 messages have been buffered
      :flush_period  => 60,      # send an email after one minute
      :level         => :error   # only process log events that are "error" or "fatal"
  )
     # File lib/logging/appenders/email.rb, line 86
 86:     def initialize( name, opts = {} )
 87:       opts[:header] = false
 88:       super(name, opts)
 89: 
 90:       af = opts.getopt(:buffsize) ||
 91:            opts.getopt(:buffer_size) ||
 92:            100
 93:       configure_buffering({:auto_flushing => af}.merge(opts))
 94: 
 95:       # get the SMTP parameters
 96:       self.from = opts.getopt :from
 97:       raise ArgumentError, 'Must specify from address' if @from.nil?
 98: 
 99:       self.to = opts.getopt :to
100:       raise ArgumentError, 'Must specify recipients' if @to.empty?
101: 
102:       self.subject   = opts.getopt :subject, "Message from #{$0}"
103:       self.address   = opts.getopt(:server) || opts.getopt(:address) || 'localhost'
104:       self.port      = opts.getopt(:port, 25)
105:       self.domain    = opts.getopt(:domain, ENV['HOSTNAME']) || 'localhost.localdomain'
106:       self.user_name = opts.getopt(:acct) || opts.getopt(:user_name)
107:       self.password  = opts.getopt(:passwd) || opts.getopt(:password)
108:       self.enable_starttls_auto = opts.getopt(:enable_starttls_auto, false)
109:       self.authentication = opts.getopt(:authtype) || opts.getopt(:authentication) || :plain
110:     end

Public Instance Methods

authentication=( val ) click to toggle source

If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain (will send the password in the clear), :login (will send password Base64 encoded) or :cram_md5 (combines a Challenge/Response mechanism to exchange information and a cryptographic Message Digest 5 algorithm to hash important information)

     # File lib/logging/appenders/email.rb, line 126
126:     def authentication=( val )
127:       @authentication = val.to_s.to_sym
128:     end
close( *args ) click to toggle source

Close the email appender. If the layout contains a foot, it will not be sent as an email.

     # File lib/logging/appenders/email.rb, line 115
115:     def close( *args )
116:       super(false)
117:     end
port=( val ) click to toggle source

On the off chance that your mail server doesn’t run on port 25, you can change it.

     # File lib/logging/appenders/email.rb, line 133
133:     def port=( val )
134:       @port = Integer(val)
135:     end
to=( val ) click to toggle source

The list of email recipients. This can either be an Array of recipients or a comma separated list. A single recipient is also valid.

  email.to = ['mike@example.com', 'tony@example.com']
  email.to = 'alicia@example.com'
  email.to = 'bob@example.com, andy@example.com, john@example.com'
     # File lib/logging/appenders/email.rb, line 144
144:     def to=( val )
145:       @to = val.respond_to?(:split) ?  val.split(',') : Array(val)
146:     end

Private Instance Methods

canonical_write( str ) click to toggle source

This method is called by the buffering code when messages need to be sent out as an email.

     # File lib/logging/appenders/email.rb, line 154
154:     def canonical_write( str )
155:       ### build a mail header for RFC 822
156:       rfc822msg =  "From: #{@from}\n"
157:       rfc822msg << "To: #{@to.join(",")}\n"
158:       rfc822msg << "Subject: #{@subject}\n"
159:       rfc822msg << "Date: #{Time.new.rfc822}\n"
160:       rfc822msg << "Message-Id: <#{"%.8f" % Time.now.to_f}@#{@domain}>\n\n"
161:       rfc822msg << str
162: 
163:       ### send email
164:       smtp = Net::SMTP.new(@address, @port)
165:       smtp.enable_starttls_auto if @enable_starttls_auto and smtp.respond_to? :enable_starttls_auto
166:       smtp.start(@domain, @user_name, @password, @authentication) { |s| s.sendmail(rfc822msg, @from, @to) }
167:       self
168:     rescue StandardError, TimeoutError => err
169:       self.level = :off
170:       ::Logging.log_internal {'e-mail notifications have been disabled'}
171:       ::Logging.log_internal(2) {err}
172:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.