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

3okqufwl  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(101)

我正在将我们使用的一个简单的java healthcheck应用程序从log4j 1转换为log4j 2 -17.2。它当前使用一个自定义SMTPAppender,它扩展了内置的SMTPAppender,以便在运行时根据记录的事件中的值设置subjectTo address
在log4j 2中无法做到这一点,因为SMTPAppender插件类是最终的。使用ThreadContext查找,使用内置的一个主题使其工作,但这对收件人地址不起作用,并努力找到无论如何设置工作的收件人地址。
这是我目前掌握的情况
受试者的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>

用这段代码设置主题,然后记录事件。这样就可以工作了,主题会出现在电子邮件中

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

对于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>

用下面的代码来设置它:

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

但是会出现下面的错误,这表明它尚未展开

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)

我做了大量的搜索来寻找这样做的方法,但没有结果。谁能告诉我这是否可以通过ThreadContext查找来完成,或者这在log4j 2中不可能用于TO地址?如果是这样,是否有其他方法的线索?
我目前的支持想法是使用log4j 2源代码制作我们自己的SMTPAppender类的副本,并修改它,但我宁愿不这样做。

8ehkhllq

8ehkhllq1#

SMTPAppender只将电子邮件的主题作为一个模式进行计算,其余字段是不可变的。
您可以使用RoutingAppender为每个电子邮件收件人创建一个不同的SMTP附加器。

<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>

相关问题