Friday, February 15th, 2008...10:43 am

Limiting log4j SMTPAppender

Jump to Comments

I wanted to set a maximum amount of e-mails that SMTPAppender would send after filling up a mailbox with 30,000 damn e-mails.

Turns out it was pretty damn easy, I set up my appender in a java class, and the SMTP appender has a method called setEvaluator that takes an instance of TriggeringEventEvaluator, so I just created a quick inner class like so.

[java]
class LimitingEvaluator implements TriggeringEventEvaluator
{
private int sentEvents = 0;
private int MAX_EVENTS = 20;
@Override
public boolean isTriggeringEvent(LoggingEvent arg0) {
sentEvents++;
if(sentEvents >= MAX_EVENTS)
return false;
return true;
}

}
[/java]

and when i set up my appender I do this
[java]
appender.setEvaluator(new LimitingEvaluator());
[/java]

1 Comment

Leave a Reply