Log4J SMTP 摘要/聚合电子邮件?

发布于 2024-09-15 03:52:10 字数 481 浏览 9 评论 0原文

我有一个 JBOSS 批处理应用程序,有时会在一分钟内向同一电子邮件地址发送数百封电子邮件,并出现 Log4J 错误。这会导致 Gmail 出现问题,因为它表示我们为该 Gmail 帐户发送电子邮件的速度太快。

所以我想知道是否有一种方法可以基本上创建一封“摘要”或“聚合”电子邮件,将所有错误日志放入一封电子邮件中,并每 5 分钟发送一次。这样一来,每 5 分钟我们可能会收到一封大电子邮件,但至少我们确实收到了电子邮件,而不是因为 gmail 服务器拒绝而延迟了几个小时。

我阅读了这篇文章,其中建议了一些有关使用评估器来做到这一点,但我看不到它是如何在 Log4J xml 配置文件中配置的。无论如何,它似乎也无法将所有日志“消化”到一封电子邮件中。

以前有人这样做过吗?或者知道是否可以?

I have a JBOSS batch application that sometimes sends hundreds on emails in a minute to the same email address with Log4J errors. This causes problems with Gmail, because it says we are sending emails too quickly for that gmail account.

So I was wondering if there was a way to basically create a "digest" or "aggregate" email puts all the error logs in 1 email and sends that every 5 minutes. So that way every 5 minutes we may get a large email, but at least we actually get the email instead of it being delayed for hours and hours by gmail servers rejecting it.

I read this post that suggested something about using an evaluator to do that, but I couldn't see how that is configured in the Log4J xml configuration file. It also seemed like it might not be able to "digest" all the logs into 1 email anyway.

Has anyone done this before? Or know if it's possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-09-22 03:52:10

来自(已存档)SMTPAppender 使用页面:

设置此属性

log4j.appender.myMail.evaluatorClass = com.mydomain.example.MyEvaluator

现在您必须创建评估器类并实现 org.apache.log4j.spi.TriggeringEventEvaluator 接口,并将此类放置在 log4j 可以访问的路径中访问它。

//Example TriggeringEventEvaluator impl

package com.mydomain.example;

import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.TriggeringEventEvaluator;

public class MyEvaluator implements TriggeringEventEvaluator {

    public boolean isTriggeringEvent(LoggingEvent event) { 
        return true; 
    }

} 

您必须在此方法中编写评估器逻辑。

From (the archived) SMTPAppender Usage page:

set this property

log4j.appender.myMail.evaluatorClass = com.mydomain.example.MyEvaluator

Now you have to create the evaluator class and implement the org.apache.log4j.spi.TriggeringEventEvaluator interface and place this class in a path where log4j can access it.

//Example TriggeringEventEvaluator impl

package com.mydomain.example;

import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.TriggeringEventEvaluator;

public class MyEvaluator implements TriggeringEventEvaluator {

    public boolean isTriggeringEvent(LoggingEvent event) { 
        return true; 
    }

} 

You have to write the evaluator logic within this method.

后知后觉 2024-09-22 03:52:10

我使用 ExtendedSmtpAppender 为 log4j2 创建了一个免费可用的解决方案。
(如果您仍然使用 log4j 1.x,只需将 log4j-1.x.jar 替换为 log4j-1.2-api-2.x.jar - 和 log4j-core-2.x.jar + log4j-api-2.x.jar 当然。)

您可以从 Maven Central 获取它 de.it-tw:log4j2-extras (这需要 Java 7+ 和 log4j 2.8+)。
如果您仅限于 Java 6(因此 log4j 2.3),则使用 de.it-tw:log4j2-Java6-extras

此外,请参阅 GitLab 项目:https://gitlab.com/thiesw/log4j2-extras (或 https://gitlab.com/thiesw/log4j2-Java6-extras)

[旧文本:
如果您使用 log4j2,请参阅其他堆栈溢出问题的答案:https://stackoverflow.com/a/34072704/5074004

或者直接转到 https 中提供的外部但公开可用的解决方案://issues.apache.org/jira/browse/LOG4J2-1192
]

I created a free useable solution for log4j2 with an ExtendedSmtpAppender.
(If you still use log4j 1.x, simply replace your log4j-1.x.jar with log4j-1.2-api-2.x.jar - and log4j-core-2.x.jar + log4j-api-2.x.jar of course.)

You get it from Maven Central as de.it-tw:log4j2-extras (This requires Java 7+ and log4j 2.8+).
If you are restricted to Java 6 (and thus log4j 2.3) then use de.it-tw:log4j2-Java6-extras

Additionally, see the GitLab project: https://gitlab.com/thiesw/log4j2-extras (or https://gitlab.com/thiesw/log4j2-Java6-extras)

[OLD text:
If you use log4j2, see answer to other stack overflow issue: https://stackoverflow.com/a/34072704/5074004

Or directly go to my external but publically available solution presented in https://issues.apache.org/jira/browse/LOG4J2-1192
]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文