Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback SMTPAppender Limiting Rate

How can I limit the rate of emails a Logback SMTPAppender, so that it would email me at most once every n minutes?

I have setup my logging according to the Logback appender, but I don't quite see how it be configured or subclassed to implement that.

Is there a hidden feature? Did someone develop a subclass to handle this?

like image 529
notnoop Avatar asked Jan 02 '10 22:01

notnoop


3 Answers

Based on the documentation it appears that the way to do this is to write an EventEvaluator (see example 4.14 and 4.15) which looks at the time stamp for each event to only accept an event when "enough time" has passed since the last event was accepted.

You can use System.currentTimeMillis to get a number you can do math on to calculate time differences. http://java.sun.com/javase/6/docs/api/java/lang/System.html#currentTimeMillis%28%29

like image 57
Thorbjørn Ravn Andersen Avatar answered Oct 07 '22 18:10

Thorbjørn Ravn Andersen


As Thorbjørn, it's easy to create an EventEvaluator that limit the rate by which an appender fires a message.

However, I found Logback to support DuplicateMessageFilter, that solves my problem probably in a bitter way: "The DuplicateMessageFilter merits a separate presentation. This filter detects duplicate messages, and beyond a certain number of repetitions, drops repeated messages."

like image 31
notnoop Avatar answered Oct 07 '22 17:10

notnoop


This tool would do exactly what you want but it's not threadsafe at all: http://code.google.com/p/throttled-smtp-appender/wiki/Usage

I've written a threadsafe version but haven't open sourced it yet.

The reason you would have trouble finding good tools for this is that SMTP isn't a real endpoint. Use a service like loggly, airbrake, or dozens of others, or run your own server using something like logstash.

like image 41
djechlin Avatar answered Oct 07 '22 18:10

djechlin