org.apache.log4j.net

Class SMTPAppender

public class SMTPAppender extends AppenderSkeleton

Send an e-mail when a specific logging event occurs, typically on errors or fatal errors.

The number of logging events delivered in this e-mail depend on the value of BufferSize option. The SMTPAppender keeps only the last BufferSize logging events in its cyclic buffer. This keeps memory requirements at a reasonable level while still delivering useful application context.

Since: 1.0

Author: Ceki Gülcü

Field Summary
protected CyclicBuffercb
protected TriggeringEventEvaluatorevaluator
protected Messagemsg
Constructor Summary
SMTPAppender()
The default constructor will instantiate the appender with a {@link TriggeringEventEvaluator} that will trigger on events with level ERROR or higher.
SMTPAppender(TriggeringEventEvaluator evaluator)
Use evaluator passed as parameter as the {@link TriggeringEventEvaluator} for this SMTPAppender.
Method Summary
voidactivateOptions()
Activate the specified options, such as the smtp host, the recipient, from, etc.
voidappend(LoggingEvent event)
Perform SMTPAppender specific appending actions, mainly adding the event to a cyclic buffer and checking if the event triggers an e-mail to be sent.
protected booleancheckEntryConditions()
This method determines if there is a sense in attempting to append.
voidclose()
intgetBufferSize()
Returns value of the BufferSize option.
StringgetEvaluatorClass()
Returns value of the EvaluatorClass option.
StringgetFrom()
Returns value of the From option.
booleangetLocationInfo()
Returns value of the LocationInfo option.
StringgetSMTPHost()
Returns value of the SMTPHost option.
StringgetSubject()
Returns value of the Subject option.
StringgetTo()
Returns value of the To option.
booleanrequiresLayout()
The SMTPAppender requires a {@link org.apache.log4j.Layout layout}.
protected voidsendBuffer()
Send the contents of the cyclic buffer as an e-mail message.
voidsetBufferSize(int bufferSize)
The BufferSize option takes a positive integer representing the maximum number of logging events to collect in a cyclic buffer.
voidsetEvaluatorClass(String value)
The EvaluatorClass option takes a string value representing the name of the class implementing the {@link TriggeringEventEvaluator} interface.
voidsetFrom(String from)
The From option takes a string value which should be a e-mail address of the sender.
voidsetLocationInfo(boolean locationInfo)
The LocationInfo option takes a boolean value.
voidsetSMTPHost(String smtpHost)
The SMTPHost option takes a string value which should be a the host name of the SMTP server that will send the e-mail message.
voidsetSubject(String subject)
The Subject option takes a string value which should be a the subject of the e-mail message.
voidsetTo(String to)
The To option takes a string value which should be a comma separated list of e-mail address of the recipients.

Field Detail

cb

protected CyclicBuffer cb

evaluator

protected TriggeringEventEvaluator evaluator

msg

protected Message msg

Constructor Detail

SMTPAppender

public SMTPAppender()
The default constructor will instantiate the appender with a {@link TriggeringEventEvaluator} that will trigger on events with level ERROR or higher.

SMTPAppender

public SMTPAppender(TriggeringEventEvaluator evaluator)
Use evaluator passed as parameter as the {@link TriggeringEventEvaluator} for this SMTPAppender.

Method Detail

activateOptions

public void activateOptions()
Activate the specified options, such as the smtp host, the recipient, from, etc.

append

public void append(LoggingEvent event)
Perform SMTPAppender specific appending actions, mainly adding the event to a cyclic buffer and checking if the event triggers an e-mail to be sent.

checkEntryConditions

protected boolean checkEntryConditions()
This method determines if there is a sense in attempting to append.

It checks whether there is a set output target and also if there is a set layout. If these checks fail, then the boolean value false is returned.

close

public void close()

getBufferSize

public int getBufferSize()
Returns value of the BufferSize option.

getEvaluatorClass

public String getEvaluatorClass()
Returns value of the EvaluatorClass option.

getFrom

public String getFrom()
Returns value of the From option.

getLocationInfo

public boolean getLocationInfo()
Returns value of the LocationInfo option.

getSMTPHost

public String getSMTPHost()
Returns value of the SMTPHost option.

getSubject

public String getSubject()
Returns value of the Subject option.

getTo

public String getTo()
Returns value of the To option.

requiresLayout

public boolean requiresLayout()
The SMTPAppender requires a {@link org.apache.log4j.Layout layout}.

sendBuffer

protected void sendBuffer()
Send the contents of the cyclic buffer as an e-mail message.

setBufferSize

public void setBufferSize(int bufferSize)
The BufferSize option takes a positive integer representing the maximum number of logging events to collect in a cyclic buffer. When the BufferSize is reached, oldest events are deleted as new events are added to the buffer. By default the size of the cyclic buffer is 512 events.

setEvaluatorClass

public void setEvaluatorClass(String value)
The EvaluatorClass option takes a string value representing the name of the class implementing the {@link TriggeringEventEvaluator} interface. A corresponding object will be instantiated and assigned as the triggering event evaluator for the SMTPAppender.

setFrom

public void setFrom(String from)
The From option takes a string value which should be a e-mail address of the sender.

setLocationInfo

public void setLocationInfo(boolean locationInfo)
The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no effort to extract the location information related to the event. As a result, the layout that formats the events as they are sent out in an e-mail is likely to place the wrong location information (if present in the format).

Location information extraction is comparatively very slow and should be avoided unless performance is not a concern.

setSMTPHost

public void setSMTPHost(String smtpHost)
The SMTPHost option takes a string value which should be a the host name of the SMTP server that will send the e-mail message.

setSubject

public void setSubject(String subject)
The Subject option takes a string value which should be a the subject of the e-mail message.

setTo

public void setTo(String to)
The To option takes a string value which should be a comma separated list of e-mail address of the recipients.
Copyright 2000-2005 Apache Software Foundation.