Class: Logging::Appenders::Email
- Inherits:
-
Logging::Appender
- Object
- Logging::Appender
- Logging::Appenders::Email
- Includes:
- Buffering
- Defined in:
- lib/logging/appenders/email.rb
Overview
Provides an appender that can send log messages via email to a list of recipients.
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#authentication ⇒ Object
Returns the value of attribute authentication.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#enable_starttls_auto ⇒ Object
Returns the value of attribute enable_starttls_auto.
-
#from ⇒ Object
Returns the value of attribute from.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#to ⇒ Object
Returns the value of attribute to.
-
#user_name ⇒ Object
Returns the value of attribute user_name.
Instance Method Summary collapse
-
#close(*args) ⇒ Object
Close the email appender.
-
#initialize(name, opts = {}) ⇒ Email
constructor
call-seq: Email.new( name, :from => ‘[email protected]’, :to => ‘[email protected]’, :subject => ‘Whoops!’ ).
Constructor Details
#initialize(name, opts = {}) ⇒ Email
call-seq:
Email.new( name, :from => '[email protected]', :to => '[email protected]', :subject => 'Whoops!' )
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 Gmail SMTP server with authentication to send out messages.
Logger.appenders.email( 'gmail',
:from => "[email protected]",
:to => "[email protected]",
:subject => "Application Error [#{%x(uname -n).strip}]",
:address => "smtp.gmail.com",
:port => 443,
:domain => "gmail.com",
:user_name => ENV["GMAIL_USER"],
:password => ENV["GMAIL_PASS"],
: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"
)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/logging/appenders/email.rb', line 85 def initialize( name, opts = {} ) opts[:header] = false super(name, opts) af = opts.fetch(:buffsize, opts.fetch(:buffer_size, 100)) configure_buffering({:auto_flushing => af}.merge(opts)) # get the SMTP parameters self.from = opts.fetch(:from, nil) raise ArgumentError, 'Must specify from address' if @from.nil? self.to = opts.fetch(:to, nil) raise ArgumentError, 'Must specify recipients' if @to.empty? self.subject = opts.fetch(:subject, "Message from #{$0}") self.address = opts.fetch(:server, opts.fetch(:address, 'localhost')) self.port = opts.fetch(:port, 25) self.domain = opts.fetch(:domain, (ENV['HOSTNAME'] || 'localhost.localdomain')) self.user_name = opts.fetch(:acct, opts.fetch(:user_name, nil)) self.password = opts.fetch(:passwd, opts.fetch(:password, nil)) self.enable_starttls_auto = opts.fetch(:enable_starttls_auto, false) self.authentication = opts.fetch(:authtype, opts.fetch(:authentication, :plain)) end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
20 21 22 |
# File 'lib/logging/appenders/email.rb', line 20 def address @address end |
#authentication ⇒ Object
Returns the value of attribute authentication.
19 20 21 |
# File 'lib/logging/appenders/email.rb', line 19 def authentication @authentication end |
#domain ⇒ Object
Returns the value of attribute domain.
20 21 22 |
# File 'lib/logging/appenders/email.rb', line 20 def domain @domain end |
#enable_starttls_auto ⇒ Object
Returns the value of attribute enable_starttls_auto.
21 22 23 |
# File 'lib/logging/appenders/email.rb', line 21 def enable_starttls_auto @enable_starttls_auto end |
#from ⇒ Object
Returns the value of attribute from.
20 21 22 |
# File 'lib/logging/appenders/email.rb', line 20 def from @from end |
#password ⇒ Object
Returns the value of attribute password.
21 22 23 |
# File 'lib/logging/appenders/email.rb', line 21 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
19 20 21 |
# File 'lib/logging/appenders/email.rb', line 19 def port @port end |
#subject ⇒ Object
Returns the value of attribute subject.
20 21 22 |
# File 'lib/logging/appenders/email.rb', line 20 def subject @subject end |
#to ⇒ Object
Returns the value of attribute to.
19 20 21 |
# File 'lib/logging/appenders/email.rb', line 19 def to @to end |
#user_name ⇒ Object
Returns the value of attribute user_name.
21 22 23 |
# File 'lib/logging/appenders/email.rb', line 21 def user_name @user_name end |
Instance Method Details
#close(*args) ⇒ Object
Close the email appender. If the layout contains a foot, it will not be sent as an email.
113 114 115 |
# File 'lib/logging/appenders/email.rb', line 113 def close( *args ) super(false) end |