views:

227

answers:

4

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?

+2  A: 

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

Thorbjørn Ravn Andersen
A: 

I suggest filing a jira item requesting this feature. It is likely to be implemented if only asked.

Ceki
A: 

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."

notnoop
A: 

Btw,

Logback v0.9.26 allows now to set the size of SMTPAppender message buffer. Until yesterday it would send the current contens of the buffer which was up to 256 messages which imho was a pain in the neck as I wanted to show only the last one in the email. Thus it's now possible to implement periodically recurring email warnings that carry only one particular error as per my interpretation of this question.

http://logback.qos.ch/manual/appenders.html#cyclicBufferSize

Have fun.

b4nd0ler0