it.openutils.log4j
Class AlternateSMTPAppender

java.lang.Object
  extended by org.apache.log4j.AppenderSkeleton
      extended by it.openutils.log4j.AlternateSMTPAppender
All Implemented Interfaces:
org.apache.log4j.Appender, org.apache.log4j.spi.OptionHandler
Direct Known Subclasses:
DynamicSubjectSMTPAppender

public class AlternateSMTPAppender
extends org.apache.log4j.AppenderSkeleton

An alternative of org.apache.log4j.net.SMTPAppender with few differences:

  <appender name="mail" class="it.openutils.log4j.AlternateSMTPAppender">
      <param name="Threshold" value="ERROR" />
      <param name="To" value="[email protected]" />
      <param name="From" value="[email protected]" />
      <param name="SMTPHost" value="localhost" />
      <param name="Timeout" value="180" />
      <param name="Subject" value="[EXAMPLE] %m" />
      <layout class="it.openutils.log4j.FilteredPatternLayout">
          <param name="ConversionPattern" value="%-5p  %c %d{dd.MM.yyyy HH:mm:ss} -- %m%n" />
          <param name="Header"
          value="
        ===================================
        Myapp (production environment)
        Date: %d{dd.MM.yyyy HH:mm:ss}
        ===================================
          " />
      </layout>
  </appender>
 

Version:
$Id: AlternateSMTPAppender.java 734 2008-03-11 15:56:29Z fgiust $
Author:
Fabrizio Giustina

Field Summary
protected  org.apache.log4j.spi.TriggeringEventEvaluator evaluator
           
protected  java.util.Map<it.openutils.log4j.LoggingEventAggregator,it.openutils.log4j.LoggingEventAggregator> events
           
protected  javax.mail.Message msg
           
 
Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold
 
Constructor Summary
AlternateSMTPAppender()
          The default constructor will instantiate the appender with a TriggeringEventEvaluator that will trigger on events with level ERROR or higher.
AlternateSMTPAppender(org.apache.log4j.spi.TriggeringEventEvaluator evaluator)
          Use evaluator passed as parameter as the TriggeringEventEvaluator for this SMTPAppender.
 
Method Summary
 void activateOptions()
          Activate the specified options, such as the smtp host, the recipient, from, etc.
 void append(org.apache.log4j.spi.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  boolean checkEntryConditions()
          This method determines if there is a sense in attempting to append.
 void close()
           
 int getBufferSize()
          Returns value of the BufferSize option.
 java.lang.String getEvaluatorClass()
          Returns value of the EvaluatorClass option.
 java.lang.String getFrom()
          Returns value of the From option.
 boolean getLocationInfo()
          Returns value of the LocationInfo option.
 java.lang.String getSMTPHost()
          Returns value of the SMTPHost option.
 java.lang.String getSubject()
          Returns value of the Subject option.
 int getTimeout()
          Returns the timeout.
 java.lang.String getTo()
          Returns value of the To option.
 boolean requiresLayout()
          The SMTPAppender requires a layout.
protected  void sendBuffer(java.util.Collection<it.openutils.log4j.LoggingEventAggregator> eventsCollection)
          Send the contents of the cyclic buffer as an e-mail message.
 void setBufferSize(int bufferSize)
          Deprecated. 
 void setEvaluator(org.apache.log4j.spi.TriggeringEventEvaluator value)
           
 void setEvaluatorClass(java.lang.String value)
          The EvaluatorClass option takes a string value representing the name of the class implementing the TriggeringEventEvaluator 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 setLocationInfo(boolean locationInfo)
          The LocationInfo option takes a boolean value.
 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 setSubject(java.lang.String subjectPattern)
          The Subject option takes a string value which should be a the subject of the e-mail message.
 void setTimeout(int timeout)
          Sets the timeout.
 void setTo(java.lang.String to)
          The To option takes a string value which should be a comma separated list of e-mail address of the recipients.
 
Methods inherited from class org.apache.log4j.AppenderSkeleton
addFilter, clearFilters, doAppend, finalize, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThreshold
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

events

protected java.util.Map<it.openutils.log4j.LoggingEventAggregator,it.openutils.log4j.LoggingEventAggregator> events

msg

protected javax.mail.Message msg

evaluator

protected org.apache.log4j.spi.TriggeringEventEvaluator evaluator
Constructor Detail

AlternateSMTPAppender

public AlternateSMTPAppender()
The default constructor will instantiate the appender with a TriggeringEventEvaluator that will trigger on events with level ERROR or higher.


AlternateSMTPAppender

public AlternateSMTPAppender(org.apache.log4j.spi.TriggeringEventEvaluator evaluator)
Use evaluator passed as parameter as the TriggeringEventEvaluator for this SMTPAppender.

Method Detail

activateOptions

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

Specified by:
activateOptions in interface org.apache.log4j.spi.OptionHandler
Overrides:
activateOptions in class org.apache.log4j.AppenderSkeleton

append

public void append(org.apache.log4j.spi.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.

Specified by:
append in class org.apache.log4j.AppenderSkeleton

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()
Specified by:
close in interface org.apache.log4j.Appender
Specified by:
close in class org.apache.log4j.AppenderSkeleton

getTo

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


requiresLayout

public boolean requiresLayout()
The SMTPAppender requires a layout.

Specified by:
requiresLayout in interface org.apache.log4j.Appender
Specified by:
requiresLayout in class org.apache.log4j.AppenderSkeleton

sendBuffer

protected void sendBuffer(java.util.Collection<it.openutils.log4j.LoggingEventAggregator> eventsCollection)
Send the contents of the cyclic buffer as an e-mail message.


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.


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.


setSubject

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


setBufferSize

@Deprecated
public void setBufferSize(int bufferSize)
Deprecated. 

This option is ignored!


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.


getSMTPHost

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


setTo

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


getBufferSize

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


setEvaluatorClass

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


setEvaluator

public void setEvaluator(org.apache.log4j.spi.TriggeringEventEvaluator value)
Parameters:
value -

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.


getLocationInfo

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


getTimeout

public int getTimeout()
Returns the timeout.

Returns:
the timeout

setTimeout

public void setTimeout(int timeout)
Sets the timeout.

Parameters:
timeout - the timeout to set


Copyright © 2008 Openmind. All Rights Reserved.