Log4j2 custom email subject from Map

好久不见. 提交于 2019-12-24 21:29:03

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/29435825/log4j2-custom-email-subject-from-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!