smtphandler
Class SMTPHandler

java.lang.Object
  extended by java.util.logging.Handler
      extended by smtphandler.SMTPHandler

public class SMTPHandler
extends java.util.logging.Handler

Send an e-mail when a specific logging record occurs, typically on WARNING or SEVERE

The number of LogRecord objects delivered in this e-mail depend on the value of bufferSize option. The SMTPHandler keeps only the last bufferSize records in its cyclic buffer. This keeps memory requirements at a reasonable level while still delivering useful application context.

The code in this class is derived from log4j's SMTPAppender class.

Author:
Ceki Gülcü (author of log4j's SMTPAppender), Sean C. Sullivan

Field Summary
protected  CyclicBuffer cb
           
protected  TriggeringRecordEvaluator evaluator
           
 
Constructor Summary
SMTPHandler()
           
 
Method Summary
protected  boolean checkEntryConditions()
          This method determines if there is a sense in attempting to append.
 void close()
           
 void flush()
           
protected  javax.mail.internet.InternetAddress getAddress(java.lang.String addressStr)
           
 int getBufferSize()
          Returns value of the bufferSize option.
protected  java.lang.String getEmailContentType()
           
 java.lang.String getEvaluatorClass()
          Returns value of the EvaluatorClass option.
 java.lang.String getFrom()
          Returns value of the From option.
 java.lang.String getSmtpHost()
          Returns value of the SmtpHost option.
 java.lang.String getSmtpPassword()
           
 java.lang.String getSmtpUsername()
           
 java.lang.String getSubject()
          Returns value of the Subject option.
 java.lang.String getTo()
          Returns value of the To option.
protected  javax.mail.internet.InternetAddress[] parseAddress(java.lang.String addressStr)
           
 void publish(java.util.logging.LogRecord record)
          Perform SMTPHandler specific appending actions, mainly adding the record to a cyclic buffer and checking if the record triggers an e-mail to be sent.
protected  void sendBuffer()
          Send the contents of the cyclic buffer as an e-mail message.
 void setBufferSize(int bufferSize)
          The bufferSize option takes a positive integer representing the maximum number of logging records to collect in a cyclic buffer.
 void setEvaluatorClass(java.lang.String value)
          The evaluatorClass option takes a string value representing the name of the class implementing the TriggeringRecordEvaluator interface.
 void setFrom(java.lang.String from)
          The from option takes a string value which should be a e-mail address of the sender.
 void setSmtpHost(java.lang.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.
 void setSmtpPassword(java.lang.String value)
           
 void setSmtpUsername(java.lang.String value)
           
 void setSubject(java.lang.String subject)
          The subject option takes a string value which should be a the subject of the e-mail message.
 void setTo(java.lang.String to)
          The to option takes a string value which should be a e-mail address of the sender.
 java.lang.String toString()
           
 
Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cb

protected CyclicBuffer cb

evaluator

protected TriggeringRecordEvaluator evaluator
Constructor Detail

SMTPHandler

public SMTPHandler()
Method Detail

publish

public void publish(java.util.logging.LogRecord record)
Perform SMTPHandler specific appending actions, mainly adding the record to a cyclic buffer and checking if the record triggers an e-mail to be sent.

Specified by:
publish in class java.util.logging.Handler

checkEntryConditions

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

It checks whether there is a "To" address set and also if there is a set evaluator. If these checks fail, then the boolean value false is returned.


close

public void close()
Specified by:
close in class java.util.logging.Handler

getAddress

protected javax.mail.internet.InternetAddress getAddress(java.lang.String addressStr)

parseAddress

protected javax.mail.internet.InternetAddress[] parseAddress(java.lang.String addressStr)

getTo

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


sendBuffer

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


getEmailContentType

protected java.lang.String getEmailContentType()

getEvaluatorClass

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


getFrom

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


getSubject

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


getSmtpHost

public java.lang.String getSmtpHost()
Returns value of the SmtpHost option.


flush

public void flush()
Specified by:
flush in class java.util.logging.Handler

getBufferSize

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


setFrom

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


setTo

public void setTo(java.lang.String to)
The to option takes a string value which should be a e-mail address of the sender.


setSubject

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


setBufferSize

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


setSmtpHost

public void setSmtpHost(java.lang.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.


setEvaluatorClass

public void setEvaluatorClass(java.lang.String value)
The evaluatorClass option takes a string value representing the name of the class implementing the TriggeringRecordEvaluator interface. A corresponding object will be instantiated and assigned as the triggering record evaluator for the SMTPHandler.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getSmtpPassword

public java.lang.String getSmtpPassword()

setSmtpPassword

public void setSmtpPassword(java.lang.String value)

getSmtpUsername

public java.lang.String getSmtpUsername()

setSmtpUsername

public void setSmtpUsername(java.lang.String value)


Copyright © 2003-2006 smtphandler.sourceforge.net. All Rights Reserved.