简体   繁体   English

Map中的Log4j2自定义电子邮件主题

[英]Log4j2 custom email subject from Map

I've some applications installed to my customers and I configured smtp appender to receive errors email. 我已经向客户安装了一些应用程序,并且我将smtp appender配置为接收错误电子邮件。

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: 而我的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>

the subject is the relevant part. 主题是相关的部分。 Unfortunally the subject is not replaced from log4j and remains as it is. 不幸的是,没有从log4j替换主题,并保持原样。

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. 当前,SmtpAppender类(实际上是其帮助程序SmtpManager)一次创建一个MimeMessage对象,并将其重新用于所有要发送的消息。 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. 我建议您针对用例在Log4j2 Jira问题跟踪器上提出功能请求。

Note: log4j 2.6+ supports this natively; 注意:log4j 2.6+本身支持此功能。 you need Java7+ for this. 为此,您需要Java7 +。

I created a free useable solution for log4j2 and also Java6 with an ExtendedSmtpAppender supporting PatternLayout in subject. 我为log4j2和Java6创建了一个免费的可用解决方案,并带有在主题中支持PatternLayout的ExtendedSmtpAppender
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. 如果仍然使用log4j 1.x(原始问题),只需将log4j-1.x.jar替换为log4j-1.2-api-2.x.jar log4j-1.x.jarlog4j-core-2.x.jar + log4j-api-2.x.jar当然是log4j-api-2.x.jar

You get it from Maven Central as de.it-tw:log4j2-extras (This requires Java 7+ and log4j 2.8+). 您可以从Maven Central以de.it-tw:log4j2-extras获取它(这需要Java 7+和log4j 2.8+)。
If you are restricted to Java 6 (and thus log4j 2.3) then use de.it-tw:log4j2-Java6-extras 如果限于Java 6(因此仅限于log4j 2.3),请使用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 ) 另请参阅GitLab项目: https ://gitlab.com/thiesw/log4j2-extras(或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. 此外,它支持突发摘要,因此在几秒钟或几分钟之内不会收到1000条错误电子邮件。 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>): 配置示例(在<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 . 另请参阅https://issues.apache.org/jira/browse/LOG4J2-1192

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM