I need to email my log file when ever my application got an error. for that i am using SMPTAppender class. but i am failing to invoke my SMTPAppender class eventually when my application is getting error.
Can any body tell me how to invoke my SMTPAppender class?
My property file is having the following properties,
log4j.properties ----------------
log4j.appender.myMail=org.apach e.log4j.net.SMTPAppender #send email if the threshold is Error and above only log4j.appender.myMail.Threshold=Error log4j.appender.myMail.BufferSize=100 log4j.appender.myMail.To=mailme@b.com log4j.appender.myMail.From=mailme@hcl.in log4j.appender.myMail.SMTPHost=mail.yahoo.com log4j.appender.myMail.Subject=COMS ProductionErrors #log4j.appender.myMail.Username=sirishg #log4j.appender.myMail. Password=gsirish log4j.appender.myMail.layout=org.apache.log4j.PatternLayout log 4j.appender.myMail.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
/** Send an e-mail when a specific logging event occurs, typically on errors or fatal errors.
<p>The number of logging events delivered in this e-mail depend on the value of <b>BufferSize</b> option. The <code>SMTPAppender</code> keeps only the last <code>BufferSize</code> logging events in its cyclic buffer. This keeps memory requirements at a reasonable level while still delivering useful application context.
<p>There are three ways in which the trigger is fired, resulting in an email containing the buffered events:
<p>* DEFAULT BEHAVIOR: relies on an internal TriggeringEventEvaluator class that triggers the sending of an email when an event with a severity of ERROR or greater is received. <p>* Set the 'evaluatorClass' param to the fully qualified class name of a class you have written that implements the TriggeringEventEvaluator interface. <p>* Set the 'expression' param to a valid (infix) expression supported by ExpressionRule and ExpressionRule's supported operators and operands.
As events are received, events are evaluated against the expression rule. An event that causes the rule to evaluate to true triggers the email send.
If both evaluatorClass and expression params are set, the evaluatorClass is used.
See org.apache.log4j.rule.ExpressionRule for a more information.
/** The default constructor will instantiate the appender with a {@link TriggeringEventEvaluator} that will trigger on events with level ERROR or higher.*/ public SMTPAppender() { this(new DefaultEvaluator()); }
/** Use <code>evaluator</code> passed as parameter as the {@link TriggeringEventEvaluator} for this SMTPAppender. */ public SMTPAppender(final TriggeringEventEvaluator evaluator) { super(false); this.evaluator = evaluator; }
/** Activate the specified options, such as the smtp host, the recipient, from, etc. */ public void activateOptions() { int errorCount = 0; Properties props = new Properties(System.getProperties());
if (smtpHost != null) { props.put("mail.smtp.host", smtpHost); }
if (subjectStr != null) { subjectLayout = new PatternLayout(); subjectLayout.setConversionPattern(subjectStr); subjectLayout.setLoggerRepository(this.repository); subjectLayout.activateOptions(); }
if (this.evaluator == null) { errorCount++; String errMsg = "No TriggeringEventEvaluator is set for appender ["+getName()+"]."; getLogger().error(errMsg); throw new IllegalStateException(errMsg); }
if (this.layout == null) { errorCount++; String errMsg = "No layout set for appender named [" + name + "]."; getLogger().error(errMsg); throw new IllegalStateException(errMsg); }
/** 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. */ public void append(LoggingEvent event) { if (!checkEntryConditions()) { return; }
event.getThreadName(); event.getNDC();
if (locationInfo) { event.getLocationInformation(); }
cb.add(event);
if (evaluator.isTriggeringEvent(event)) { sendBuffer(event); } }
/** This method determines if there is a sense in attempting to append.
<p>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 <code>false</code> is returned. */ protected boolean checkEntryConditions() { if (this.msg == null) { return false; }
if (this.evaluator == null) { return false; }
if (this.layout == null) { return false; }
return true; }
public synchronized void close() { this.closed = true; }
/** Returns value of the <b>To</b> option. */ public String getTo() { return to; }
/** The <code>SMTPAppender</code> requires a {@link org.apache.log4j.Layout layout}. */ public boolean requiresLayout() { return true; }
/** Send the contents of the cyclic buffer as an e-mail message. */ protected void sendBuffer(LoggingEvent triggeringEvent) { // Note: this code already owns the monitor for this // appender. This frees us from needing to synchronize on 'cb'. try { MimeBodyPart part = new MimeBodyPart();
/** Returns value of the <b>EvaluatorClass</b> option. */ public String getEvaluatorClass() { return (evaluator == null) ? null : evaluator.getClass().getName(); }
/** Returns value of the <b>From</b> option. */ public String getFrom() { return from; }
/** Returns value of the <b>Subject</b> option. */ public String getSubject() { return subjectStr; }
/** The <b>From</b> option takes a string value which should be a e-mail address of the sender. */ public void setFrom(String from) { this.from = from; }
/** * The <b>Subject</b> option takes a string value which will be the subject * of the e-mail message. This value can be string literal or a conversion * pattern in the same format as expected by * {@link org.apache.log4j.PatternLayout}. * * <p>The conversion pattern is applied on the triggering event to dynamically * compute the subject of the outging email message. For example, setting * the <b>Subject</b> option to "%properties{host} - %m" * will set the subject of outgoing message to the "host" property of the * triggering event followed by the message of the triggering event. */ public void setSubject(String subject) { this.subjectStr = subject; }
/** The <b>BufferSize</b> option takes a positive integer representing the maximum number of logging events to collect in a cyclic buffer. When the <code>BufferSize</code> 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. */ public void setBufferSize(int bufferSize) { this.bufferSize = bufferSize; cb.resize(bufferSize); }
/** The <b>SMTPHost</b> option takes a string value which should be a the host name of the SMTP server that will send the e-mail message. */ public void setSMTPHost(String smtpHost) { this.smtpHost = smtpHost; }
/** Returns value of the <b>SMTPHost</b> option. */ public String getSMTPHost() { return smtpHost; }
/** The <b>To</b> option takes a string value which should be a comma separated list of e-mail address of the recipients. */ public void setTo(String to) { this.to = to; }
/** Returns value of the <b>BufferSize</b> option. */ public int getBufferSize() { return bufferSize; }
/** * The <b>EvaluatorClass</b> 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. * * @deprecated replaced by {@link #setEvaluator}. */ public void setEvaluatorClass(String value) { getLogger().warn("The SMPTAppender.setEvaluatorClass is deprecated."); getLogger().warn("It has been replaced with the more powerful SMPTAppender.setEvaluator method."); evaluator = (TriggeringEventEvaluator) OptionConverter.instantiateByClassName( value, TriggeringEventEvaluator.class, evaluator); }
/** * Set {@link TriggeringEventEvaluator} for this instance of SMTPAppender. */ public void setEvaluator(TriggeringEventEvaluator evaluator) { this.evaluator = evaluator; }
/** The <b>LocationInfo</b> 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).
<p>Location information extraction is comparatively very slow and should be avoided unless performance is not a concern. */ public void setLocationInfo(boolean locationInfo) { this.locationInfo = locationInfo; }
/** Returns value of the <b>LocationInfo</b> option. */ public boolean getLocationInfo() { return locationInfo; }
/** * Set charset for messages: ensure the charset * you are using is available on your platform. */ public void setCharset(String charset) { this.charset = charset; }
/** * Returns the charset for messages. The default * is "ISO-8859-1." This method should not return * null. */ public String getCharset() { return charset; }
}
class DefaultEvaluator extends ComponentBase implements TriggeringEventEvaluator {
public void setExpression(String expression) { this.expression = expression; }
public void activateOptions() { if(expression != null) { try { expressionRule = ExpressionRule.getRule(expression); } catch (IllegalArgumentException iae) { getLogger().error("Unable to use provided expression - falling back to default behavior (trigger on ERROR or greater severity)", iae); } } }
/** Is this <code>event</code> the e-mail triggering event?
<p>This method returns <code>true</code>, if the event level has ERROR level or higher. Otherwise it returns <code>false</code>. */ public boolean isTriggeringEvent(LoggingEvent event) { if (expressionRule == null) { return event.getLevel().isGreaterOrEqual(Level.ERROR); } return expressionRule.evaluate(event); } }