Log4J: Emailing specific errors only

Log4J’s SMTPAppender provides enough basic functionality to send out error messages as emails. Although emailing error messages is not always a good idea, unless something really goes wrong in system. Sometimes it is required to send out only specific errors in specific areas of the system. It is possible to do that with the SMTPAppender, even if these specific errors lie in the same category(WARN, ERROR etc.) as other errors. This is achieved with TriggeringEventEvaluator. Here is a sample config for the SMTPAppender.

<appender name="EMAIL" class="org.apache.log4j.net.SMTPAppender">
<param name="To" value="kamran.zafar@xeustechnologies.org" />
<param name="From" value="server-errors@xeustechnologies.org" />
<param name="Subject" value="SERIOUS ERROR" />
<param name="SMTPHost" value="mysmtphost" />
<param name="SMTPUsername" value="server-errors" />
<param name="SMTPPassword" value="password" />
<param name="Threshold" value="ERROR" />
<param name="BufferSize" value="1" />
<param name="EvaluatorClass" value="org.xeustechnologies.test.log4j.SmtpTrigger" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss.SSS}][%p][%t][%c] - %m%n%n" />

The parameter EvaluatorClass is where we specify the implementation of TriggeringEventEvaluator, which acts like a filter to allow emailing only specific errors:

import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.TriggeringEventEvaluator;

public class SmtpTrigger implements TriggeringEventEvaluator {

public boolean isTriggeringEvent(LoggingEvent event) {
* Email errors
if( event.getLoggerName().equals( "SomeSeriousErrorLogger" )
&amp;&amp; event.getLevel().equals( Level.ERROR ) ) {
return true;

return false;

Now only ERRORs from “SomeSeriousErrorLogger” will be emailed.

Leave a Reply