Methods Summary |
---|
public void | activateOptions()Activate the specified options, such as the smtp host, the
recipient, from, etc.
Session session = createSession();
msg = new MimeMessage(session);
try {
addressMessage(msg);
if(subject != null) {
msg.setSubject(subject);
}
} catch(MessagingException e) {
LogLog.error("Could not activate SMTPAppender options.", e );
}
if (evaluator instanceof OptionHandler) {
((OptionHandler) evaluator).activateOptions();
}
|
protected void | addressMessage(javax.mail.Message msg)Address message.
if (from != null) {
msg.setFrom(getAddress(from));
} else {
msg.setFrom();
}
if (to != null && to.length() > 0) {
msg.setRecipients(Message.RecipientType.TO, parseAddress(to));
}
//Add CC receipients if defined.
if (cc != null && cc.length() > 0) {
msg.setRecipients(Message.RecipientType.CC, parseAddress(cc));
}
//Add BCC receipients if defined.
if (bcc != null && bcc.length() > 0) {
msg.setRecipients(Message.RecipientType.BCC, parseAddress(bcc));
}
|
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.
if(!checkEntryConditions()) {
return;
}
event.getThreadName();
event.getNDC();
event.getMDCCopy();
if(locationInfo) {
event.getLocationInformation();
}
cb.add(event);
if(evaluator.isTriggeringEvent(event)) {
sendBuffer();
}
|
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.
if(this.msg == null) {
errorHandler.error("Message object not configured.");
return false;
}
if(this.evaluator == null) {
errorHandler.error("No TriggeringEventEvaluator is set for appender ["+
name+"].");
return false;
}
if(this.layout == null) {
errorHandler.error("No layout set for appender named ["+name+"].");
return false;
}
return true;
|
public synchronized void | close()
this.closed = true;
|
protected javax.mail.Session | createSession()Create mail session.
Properties props = null;
try {
props = new Properties (System.getProperties());
} catch(SecurityException ex) {
props = new Properties();
}
if (smtpHost != null) {
props.put("mail.smtp.host", smtpHost);
}
Authenticator auth = null;
if(smtpPassword != null && smtpUsername != null) {
props.put("mail.smtp.auth", "true");
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUsername, smtpPassword);
}
};
}
Session session = Session.getInstance(props, auth);
if (smtpDebug) {
session.setDebug(smtpDebug);
}
return session;
|
javax.mail.internet.InternetAddress | getAddress(java.lang.String addressStr)
try {
return new InternetAddress(addressStr);
} catch(AddressException e) {
errorHandler.error("Could not parse address ["+addressStr+"].", e,
ErrorCode.ADDRESS_PARSE_FAILURE);
return null;
}
|
public java.lang.String | getBcc()Get the bcc recipient addresses.
return bcc;
|
public int | getBufferSize()Returns value of the BufferSize option.
return bufferSize;
|
public java.lang.String | getCc()Get the cc recipient addresses.
return cc;
|
public final org.apache.log4j.spi.TriggeringEventEvaluator | getEvaluator()Get triggering evaluator.
return evaluator;
|
public java.lang.String | getEvaluatorClass()Returns value of the EvaluatorClass option.
return evaluator == null ? null : evaluator.getClass().getName();
|
public java.lang.String | getFrom()Returns value of the From option.
return from;
|
public boolean | getLocationInfo()Returns value of the LocationInfo option.
return locationInfo;
|
public boolean | getSMTPDebug()Get SMTP debug.
return smtpDebug;
|
public java.lang.String | getSMTPHost()Returns value of the SMTPHost option.
return smtpHost;
|
public java.lang.String | getSMTPPassword()Get SMTP password.
return smtpPassword;
|
public java.lang.String | getSMTPUsername()Get SMTP user name.
return smtpUsername;
|
public java.lang.String | getSubject()Returns value of the Subject option.
return subject;
|
public java.lang.String | getTo()Returns value of the To option.
return to;
|
javax.mail.internet.InternetAddress[] | parseAddress(java.lang.String addressStr)
try {
return InternetAddress.parse(addressStr, true);
} catch(AddressException e) {
errorHandler.error("Could not parse address ["+addressStr+"].", e,
ErrorCode.ADDRESS_PARSE_FAILURE);
return null;
}
|
public boolean | parseUnrecognizedElement(org.w3c.dom.Element element, java.util.Properties props){@inheritDoc}
if ("triggeringPolicy".equals(element.getNodeName())) {
Object triggerPolicy =
org.apache.log4j.xml.DOMConfigurator.parseElement(
element, props, TriggeringEventEvaluator.class);
if (triggerPolicy instanceof TriggeringEventEvaluator) {
setEvaluator((TriggeringEventEvaluator) triggerPolicy);
}
return true;
}
return false;
|
public boolean | requiresLayout()The SMTPAppender requires a {@link
org.apache.log4j.Layout layout}.
return true;
|
protected void | sendBuffer()Send the contents of the cyclic buffer as an e-mail message.
// Note: this code already owns the monitor for this
// appender. This frees us from needing to synchronize on 'cb'.
try {
MimeBodyPart part = new MimeBodyPart();
StringBuffer sbuf = new StringBuffer();
String t = layout.getHeader();
if(t != null)
sbuf.append(t);
int len = cb.length();
for(int i = 0; i < len; i++) {
//sbuf.append(MimeUtility.encodeText(layout.format(cb.get())));
LoggingEvent event = cb.get();
sbuf.append(layout.format(event));
if(layout.ignoresThrowable()) {
String[] s = event.getThrowableStrRep();
if (s != null) {
for(int j = 0; j < s.length; j++) {
sbuf.append(s[j]);
sbuf.append(Layout.LINE_SEP);
}
}
}
}
t = layout.getFooter();
if(t != null)
sbuf.append(t);
part.setContent(sbuf.toString(), layout.getContentType());
Multipart mp = new MimeMultipart();
mp.addBodyPart(part);
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
} catch(Exception e) {
LogLog.error("Error occured while sending e-mail notification.", e);
}
|
public void | setBcc(java.lang.String addresses)Set the bcc recipient addresses.
this.bcc = addresses;
|
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.
this.bufferSize = bufferSize;
cb.resize(bufferSize);
|
public void | setCc(java.lang.String addresses)Set the cc recipient addresses.
this.cc = addresses;
|
public final void | setEvaluator(org.apache.log4j.spi.TriggeringEventEvaluator trigger)Sets triggering evaluator.
if (trigger == null) {
throw new NullPointerException("trigger");
}
this.evaluator = trigger;
|
public void | setEvaluatorClass(java.lang.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.
evaluator = (TriggeringEventEvaluator)
OptionConverter.instantiateByClassName(value,
TriggeringEventEvaluator.class,
evaluator);
|
public void | setFrom(java.lang.String from)The From option takes a string value which should be a
e-mail address of the sender.
this.from = from;
|
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.
this.locationInfo = locationInfo;
|
public void | setSMTPDebug(boolean debug)Setting the SmtpDebug option to true will cause the mail session to log its server interaction to stdout.
This can be useful when debuging the appender but should not be used during production because username and
password information is included in the output.
this.smtpDebug = debug;
|
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.
this.smtpHost = smtpHost;
|
public void | setSMTPPassword(java.lang.String password)The SmtpPassword option takes a string value which should be the password required to authenticate against
the mail server.
this.smtpPassword = password;
|
public void | setSMTPUsername(java.lang.String username)The SmtpUsername option takes a string value which should be the username required to authenticate against
the mail server.
this.smtpUsername = username;
|
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.
this.subject = subject;
|
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.
this.to = to;
|