1

I set up a Spring-boot application, which sends every 24 hours maximum 10 logs to a specific e-mail when exceptions are thrown. I use logback it works but after implementing a custom SMTPAdapter (to limit the e-mail rate) the HTMLLayout in the e-mail has only titles, no content, what do I have to change in my custom SMTPAppender?

IDEA:
Using the default works, after research I found out that I have to set the attibutes (Date...) in the custom appender but I don't find any solutions.

IMAGE:

E-mail

For the custom SMTPAppender i used this code:
https://stackoverflow.com/a/31081552/6865330

Code of the ScheduledSMTPAppender:

public class ScheduledSMTPAppender extends SMTPAppender {

private final ThreadFactory tf = r -> {
    Thread t = new Thread(r, "ScheduledSMTPAppender Thread");
    t.setDaemon(true); //make daemon or it will prevent the program to exit
    return t;
};
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, tf);

private final List<ILoggingEvent> events = new ArrayList<>();

private int maxMessages = 10;

public ScheduledSMTPAppender() {
    super();
}

public ScheduledSMTPAppender(EventEvaluator<ILoggingEvent> eventEvaluator) {
    super(eventEvaluator);
}

@Override public void start() {
    super.start();
    scheduler.scheduleAtFixedRate(this::sendEmail, 1, 1, TimeUnit.MINUTES);
}

@Override protected void sendBuffer(CyclicBuffer<ILoggingEvent> cb, ILoggingEvent lastEventObject) {
    if (events.size() > maxMessages) sendEmail();
}

private synchronized void sendEmail() {
    try {
        if (events.isEmpty()) return;
        ILoggingEvent lastEvent = events.get(events.size() - 1);
        events.remove(events.size() - 1);
        CyclicBuffer<ILoggingEvent> cb;

        if (events.isEmpty()) {
            cb = new CyclicBuffer<>(1);
        } else {
            cb = new CyclicBuffer<>(events.size());
            for (ILoggingEvent e : events){
                cb.add(e);
            }
        }
        super.sendBuffer(cb, lastEvent);
        events.clear();
    } catch (Exception e) {
        //Important to have a catch all here or the scheduled task will die
        addError("Error occurred while sending e-mail notification.", e);
    }
}

//this allows to make "maxMessages" a parameter of your appender
public int getMaxMessages() { return maxMessages; }

public void setMaxMessages(int maxMessages) { this.maxMessages = maxMessages; }

}

Marko Previsic
  • 1,820
  • 16
  • 30
Alan
  • 589
  • 5
  • 29

0 Answers0