Logback SMTPAppender 限制速率
如何限制 Logback 的电子邮件速率 SMTPAppender
,以便它最多每 n 分钟向我发送一次电子邮件?
我已经根据 Logback 附加程序 设置了日志记录,但我不太清楚看看如何配置或子类化它来实现它。
有隐藏功能吗?有人开发了一个子类来处理这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据文档,执行此操作的方法似乎是编写一个 EventEvaluator(参见示例 4.14 和 4.15),它查看每个事件的时间戳,以便仅在自上次事件发生以来已经过去“足够的时间”时接受事件。公认。
您可以使用 System.currentTimeMillis 获取一个数字,您可以对其进行数学计算来计算时间差。 http://java.sun .com/javase/6/docs/api/java/lang/System.html#currentTimeMillis%28%29
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,很容易创建一个
EventEvaluator
来限制附加程序触发消息的速率。但是,我发现 Logback 支持
DuplicateMessageFilter
,即可能以一种痛苦的方式解决了我的问题:“DuplicateMessageFilter 值得单独介绍。该过滤器检测重复消息,超过一定数量的重复,就会删除重复的消息。”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."看看新的 Whisper 附加器。它进行智能抑制。可通过 Maven 和 github 获取 此处
法定免责声明:我是作者。
Have a look at the new Whisper appender. It does smart suppression. Available via Maven and github here
Statutory disclaimer: I'm the author.
该工具可以完全满足您的要求,但它根本不是线程安全的:http: //code.google.com/p/throttled-smtp-appender/wiki/Usage
我已经编写了一个线程安全版本,但尚未开源。
您很难找到合适的工具来实现此目的的原因是 SMTP 不是真正的端点。使用 loggly、airbrake 或其他数十种服务,或者使用 Logstash 等工具运行您自己的服务器。
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.
为了解决同样的问题,我编写了自定义评估器。它扩展了 ch.qos.logback.classic.boolex.OnMarkerEvaluator,但您可以使用任何其他评估器作为基础。如果静默间隔中有许多可接受的消息,评估器将丢弃这些消息。对于我的用例来说,这是可以的,但如果您需要不同的行为 - 只需在第二个 if 中添加额外的检查即可。
配置为每 1000 秒(大约 17 分钟)发送最多一条消息:
To solve same problem I've written custom evaluator. It extends ch.qos.logback.classic.boolex.OnMarkerEvaluator, but you can use any other evaluator as base. If there will many acceptable messages in silence interval evaluator will discard these. For my use case it's ok, but if you need different behavior - just add extra checks to the second if.
Config to send maximum one message every 1000 second (about 17 mins):
我建议提交一个请求此功能的 jira 项目。只要提出要求,就有可能实施。
I suggest filing a jira item requesting this feature. It is likely to be implemented if only asked.
顺便说一句,
Logback v0.9.26 现在允许设置 SMTPAppender 消息缓冲区的大小。直到昨天,它都会发送缓冲区的当前内容,最多可达 256 条消息,恕我直言,这让我很头疼,因为我只想显示电子邮件中的最后一条消息。因此,现在可以实现定期重复的电子邮件警告,根据我对此问题的解释,该警告仅携带一个特定错误。
玩得开心。
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.