2

I'm using an SMTPAppender in Logback to send messages from automated jobs when they complete successfully and when they encounter errors. I'd like the emails notifying us about errors to be sent with high importance. How can this be done with Logback/SMPTAppender? If it can't be done out of the box, does anyone have a home made solution for this problem to share?

Jocke Berg
  • 51
  • 5
  • I don't think there's a parameter that does this, but if you're ready to write a custom appender, I figure it should be simple enough. You need to extend the standard one and then set the header of the mime message object. – Darius X. Jun 12 '13 at 19:35

2 Answers2

3

I had a look at extending the appender and ended up with this, which seems to do the job.

import javax.mail.MessagingException;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.net.SMTPAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.helpers.CyclicBuffer;

public class PrioritySMPTAppender extends SMTPAppender {

    @Override
    protected void sendBuffer(CyclicBuffer<ILoggingEvent> cyclicBuffer, ILoggingEvent lastEventObject) {
        try {
            // 1,2 = high, 3 = normal, 4-5 = low, apparently. Most readers won't make
            // a difference between 1 and 2, or 4 and 5, allegedly.
            if(lastEventObject.getLevel().isGreaterOrEqual(Level.WARN)) {
                mimeMsg.setHeader("X-Priority", "1");
            } else {
                mimeMsg.setHeader("X-Priority", "3");
            }
        } catch(MessagingException e) {}
        super.sendBuffer(cyclicBuffer, lastEventObject);
    }   
}

This is for internal use only and we don't care much about handling the exception properly and/or adding properties for log levels and priorities but others might want to.

Jocke Berg
  • 51
  • 5
0

try this, Hope it will resolve your issue :

https://github.com/abdulwaheed18/Slf4jTutorial/blob/master/src/com/waheed/tutorial8/Application8.java

Waheed
  • 1,835
  • 15
  • 21
  • Maybe I'm missing something obvious here but I can't see where you're doing anything to the priority of the emails that are being sent. – Jocke Berg Jul 17 '13 at 14:13