Log4j2自定义地图的电子邮件主题[英] Log4j2 custom email subject from Map

本文是小编为大家收集整理的关于Log4j2自定义地图的电子邮件主题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经为客户安装了一些应用程序,我配置了SMTP Appender以接收错误电子邮件.

不幸的是,我需要一种方法来了解客户从哪些客户到达电子邮件.

我正在尝试在地图中设置一个参数,以将其显示为电子邮件的主题.我只能在启动应用程序并启动数据库后设置此参数:

String[] parametri = {username};
MapLookup.setMainArguments(parametri);

和我的log4j2.xml是:

<SMTP name="Mailer" subject="${sys:logPath} - ${map:0}" to="${receipients}"
        from="${from}" smtpHost="${smtpHost}" smtpPort="${smtpPort}"
        smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
        smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="200"
        ignoreExceptions="false">
    </SMTP>

主题是相关部分.不幸的是,该主题未从log4j替换并保持原样.

我做错了什么?

谢谢

推荐答案

当前,SMTPAppender类(实际上是其助手SMTPManager)一次创建一个模拟对象,并重新将其重新以发送所有消息.消息主题仅初始化一次.查找仅在读取配置时仅完成一次.

我建议您在log4j2 jira问题跟踪器上提出功能请求.

其他推荐答案

注意:log4j 2.6+本地支持这一点;您需要Java7+.

我使用 ExtendedSmtpAppender 在主题中为 ExtendedSmtpAppender 提供了一个免费的可用解决方案.
如果您仍然使用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项目: a>(或 >)


此外,它支持爆发总结,因此您将在几秒钟或几分钟内不会收到1000个错误电子邮件.用例:通过电子邮件将所有错误记录发送到支持/开发人员.在破损的网络或数据库上,这可能会导致数百个相同的错误电子邮件. 此appender执行以下操作:

  • 第一次出现会立即通过电子邮件发送
  • 所有以下类似的错误日志都在一定时间内进行缓冲(相似性和时间是可配置的)
  • 在过去的时间之后,一封摘要电子邮件(事件数量,时间)以及第一个和最后一个事件是发送

示例配置(内部):

<SMTPx name="ErrorMail" smtpHost="mailer.xxxx.de" smtpPort="25"
        from="your name &lt;noReply@xxx.de>"  to="${errorEmailAddresses}"
        subject="[PROJECT-ID, ${hostName}, ${web:contextPath}] %p: %c{1} - %m%notEmpty{ =>%ex{short})}"
        subjectWithLayout="true"  bufferSize="5"
        burstSummarizingSeconds="300" bsCountInSubject="S" bsMessageMaskDigits="true"
        bsExceptionOrigin="true" >
    <PatternLayout pattern="-- %d  %p  %c [%.20t,%x]  %m%n" charset="UTF-8" />      <!-- SMTP uses fixed charset for message -->
</SMTPx>
<Async name="AsyncErrorMail" blocking="false" errorRef="Console">
    <AppenderRef ref="ErrorMail"/>
</Async>

另请参阅 -1192 .

本文地址:https://www.itbaoku.cn/post/1575155.html

问题描述

I've some applications installed to my customers and I configured smtp appender to receive errors email.

Unfortunally I need a way to understand from which customer is arriving the email.

I'm trying to set a parameter in the map in order to show it as the subject of the email. I can set this parameter only after my app is started and the db is up:

String[] parametri = {username};
MapLookup.setMainArguments(parametri);

and my log4j2.xml is:

<SMTP name="Mailer" subject="${sys:logPath} - ${map:0}" to="${receipients}"
        from="${from}" smtpHost="${smtpHost}" smtpPort="${smtpPort}"
        smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
        smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="200"
        ignoreExceptions="false">
    </SMTP>

the subject is the relevant part. Unfortunally the subject is not replaced from log4j and remains as it is.

What I'm doing wrong?

Thanks

推荐答案

Currently, the SmtpAppender class (actually its helper SmtpManager) creates a MimeMessage object once and reuses it for all messages to be sent. The message subject is initialized only once. The lookup is done only once when your configuration is read.

I suggest you raise a feature request on the Log4j2 Jira issue tracker for your use case.

其他推荐答案

Note: log4j 2.6+ supports this natively; you need Java7+ for this.

I created a free useable solution for log4j2 and also Java6 with an ExtendedSmtpAppender supporting PatternLayout in subject.
If you still use log4j 1.x (original question), 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

See also the GitLab project: https://gitlab.com/thiesw/log4j2-extras (or https://gitlab.com/thiesw/log4j2-Java6-extras)


Additionally, it supports burst summarizing, so you will not get 1000 error emails within a few seconds or minutes. Use case: Send all ERROR-logs via Email to support/developer. On a broken network or database this can cause hundreds of the same error email. This appender does the following:

  • the first occurrence is emailed immediately
  • all following similar ERROR logs are buffered for a certain time (similarity and time is configurable)
  • after the time passed, a summary email with summary info (number of events, time) and the first and last event is send

Example configuration (inside <Appenders>):

<SMTPx name="ErrorMail" smtpHost="mailer.xxxx.de" smtpPort="25"
        from="your name &lt;noReply@xxx.de>"  to="${errorEmailAddresses}"
        subject="[PROJECT-ID, ${hostName}, ${web:contextPath}] %p: %c{1} - %m%notEmpty{ =>%ex{short})}"
        subjectWithLayout="true"  bufferSize="5"
        burstSummarizingSeconds="300" bsCountInSubject="S" bsMessageMaskDigits="true"
        bsExceptionOrigin="true" >
    <PatternLayout pattern="-- %d  %p  %c [%.20t,%x]  %m%n" charset="UTF-8" />      <!-- SMTP uses fixed charset for message -->
</SMTPx>
<Async name="AsyncErrorMail" blocking="false" errorRef="Console">
    <AppenderRef ref="ErrorMail"/>
</Async>

See also https://issues.apache.org/jira/browse/LOG4J2-1192.