Search code examples
javasmtplog4jaggregatedigest

Log4J SMTP digest/aggregate emails?


I have a JBOSS batch application that sometimes sends hundreds on emails in a minute to the same email address with Log4J errors. This causes problems with Gmail, because it says we are sending emails too quickly for that gmail account.

So I was wondering if there was a way to basically create a "digest" or "aggregate" email puts all the error logs in 1 email and sends that every 5 minutes. So that way every 5 minutes we may get a large email, but at least we actually get the email instead of it being delayed for hours and hours by gmail servers rejecting it.

I read this post that suggested something about using an evaluator to do that, but I couldn't see how that is configured in the Log4J xml configuration file. It also seemed like it might not be able to "digest" all the logs into 1 email anyway.

Has anyone done this before? Or know if it's possible?


Solution

  • From (the archived) SMTPAppender Usage page:

    set this property

    log4j.appender.myMail.evaluatorClass = com.mydomain.example.MyEvaluator
    

    Now you have to create the evaluator class and implement the org.apache.log4j.spi.TriggeringEventEvaluator interface and place this class in a path where log4j can access it.

    //Example TriggeringEventEvaluator impl
    
    package com.mydomain.example;
    
    import org.apache.log4j.spi.LoggingEvent;
    import org.apache.log4j.spi.TriggeringEventEvaluator;
    
    public class MyEvaluator implements TriggeringEventEvaluator {
    
        public boolean isTriggeringEvent(LoggingEvent event) { 
            return true; 
        }
    
    } 
    

    You have to write the evaluator logic within this method.