简体   繁体   English

我如何在运行时使用 log4j2 SMTPAppender 设置 TO 地址

[英]How can i set TO address at runtime with log4j2 SMTPAppender

I'm converting a simple java healthcheck application we use from using log4j1 to log4j2-17.2.我正在将我们使用的一个简单的 java 健康检查应用程序从使用 log4j1 转换为 log4j2-17.2。 It currently uses a custom SMTPAppender which extends the built in one to set subject and To address at runtime from values in the logged event.它目前使用一个自定义的 SMTPAppender,它扩展了内置的 SMTPAppender 以在运行时根据记录的事件中的值设置主题地址

In log4j2 can't do that as the SMTPAppender plugin class is final.在 log4j2 中不能这样做,因为 SMTPAppender 插件 class 是最终的。 Have got it working using the built in one for subject using a ThreadContext lookup, but that doesn't work for TO address and struggling to find anyway to set the To address that works.使用 ThreadContext 查找使用内置的主题使其工作,但这不适用于 TO 地址并且无论如何都很难找到以设置有效的 To 地址。

This is what I have so far这是我到目前为止所拥有的

For subject the SMTPAppender config is:对于主题,SMTPAppender 配置为:

<SMTP   name="ErrorMail"
                subject="$${ctx:subject:-Int HealthCheck Error}" 
                to="user@company.com" 
                from="support@company.com"
                smtpHost="ldnsmtp" 
                smtpPort="25" 
                bufferSize="2000" 
                ignoreExceptions="true">
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <EmailLoggerLayout />               
          </SMTP>

With this code setting the subject then logging the event.使用此代码设置主题然后记录事件。 This works and the subject appears in the email OK这有效,主题出现在 email OK

            if (healthCheckAlert.getSubject() != null) {
                ThreadContext.put("subject", subjectPrefix + healthCheckAlert.getSubject());
            }
            warningSender.log(level, message);

For the To address I'm trying this to use a ThreadContext variable in same way:对于 To 地址,我正在尝试以相同的方式使用 ThreadContext 变量:

         <SMTP  name="WarningMail" 
                subject="$${ctx:subject:-Int HealthCheck Warning}"
                to="$${ctx:mailto}" 
                from="support@company.com"
                smtpHost="ldnsmtp" 
                smtpPort="25" 
                bufferSize="2000" 
                ignoreExceptions="true">
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> 
                <EmailLoggerLayout />               
         </SMTP>

With this code to set it:使用此代码进行设置:

        if (healthCheckAlert.getMailTo() != null) {
            ThreadContext.put("mailto", healthCheckAlert.getMailTo());
        }

But get the error below, which indicates it hasn't been expanded但是得到下面的错误,说明还没有展开

2023-01-05 12:40:25,951 main ERROR SmtpManager SMTP:${ctx:mailto}:::support@company.com::${ctx:subject:-Int HealthCheck Warning}:smtp:ldnsmtp:25:::INFO Could not set SmtpAppender message options: javax.mail.internet.AddressException: Local address contains illegal character in string ``${ctx:mailto}'' javax.mail.internet.AddressException: Local address contains illegal character in string ``${ctx:mailto}''
    at javax.mail.internet.InternetAddress.checkAddress(InternetAddress.java:1216)
    at javax.mail.internet.InternetAddress.parse(InternetAddress.java:1091)
    at javax.mail.internet.InternetAddress.parse(InternetAddress.java:633)
    at org.apache.logging.log4j.core.net.MimeMessageBuilder.parseAddresses(MimeMessageBuilder.java:99)
    at org.apache.logging.log4j.core.net.MimeMessageBuilder.setRecipients(MimeMessageBuilder.java:66)
    at org.apache.logging.log4j.core.net.SmtpManager.createMimeMessage(SmtpManager.java:71)
    at org.apache.logging.log4j.core.net.SmtpManager.connect(SmtpManager.java:340)
    at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
    at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:353)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:161)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:134)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:125)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:89)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:675)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:633)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:616)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:552)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:82)
    at org.apache.logging.log4j.core.Logger.log(Logger.java:161)
    at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2205)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2159)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2142)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1994)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1852)
    at org.apache.logging.log4j.spi.AbstractLogger.log(AbstractLogger.java:1642)
    at com.company.utils.healthcheck.logging.EmailLogger.log(EmailLogger.java:42)
    at com.company.utils.healthcheck.EmailLoggerTest.testWarnAlert(EmailLoggerTest.java:42)

Have done a lot of searching to find a way to do this to no avail.已经做了很多搜索以找到一种方法来做到这一点无济于事。 Can anyone let me know if this can be done with the ThreadContext lookup somehow, or is that just not possible in log4j2 for the TO address?谁能告诉我这是否可以通过 ThreadContext 查找以某种方式完成,或者在 log4j2 中不可能用于 TO 地址? If so any clues on some other way to do this?.如果是的话,有什么其他方法可以做到这一点的线索吗?

My current backstop idea is to make our own copy of the SMTPAppender class using the log4j2 source code and modify that, but would rather not.我目前的支持想法是使用 log4j2 源代码制作我们自己的 SMTPAppender class 副本并修改它,但我宁愿不这样做。

The SMTPAppender only evaluates the subject of an e-mail as a pattern . SMTPAppender仅将电子邮件的主题评估为模式 The remaining fields are immutable.其余字段是不可变的。

What you can do is to use RoutingAppender to create a different SMTP appender per e-mail recipient.您可以做的是使用RoutingAppender为每个电子邮件收件人创建一个不同的 SMTP appender。 Something like this should work (I didn't test it):像这样的东西应该可以工作(我没有测试过):

<Routing name="Routing">
  <Routes pattern="$${ctx:mailto}">
    <Route>
         <SMTP  name="WarningMail" 
                subject="$${ctx:subject:-Int HealthCheck Warning}"
                to="${ctx:mailto}" 
                from="support@company.com"
                smtpHost="ldnsmtp" 
                smtpPort="25" 
                bufferSize="2000" 
                ignoreExceptions="true">
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> 
                <EmailLoggerLayout />               
         </SMTP>
    </Route>
  </Routes>
  <IdlePurgePolicy timeToLive="15" timeUnit="minutes"/>
</Routing>

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

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