我正在尝试将Log4j2配置为在发生错误时发送电子邮件。到目前为止,它正在发送它们而没有任何错误,但是主体始终为空。 Log4j2 v2.11.1,JavaMail v1.6.2和Tomcat 7.0.43就是这种情况。
我已经尝试使用各种布局(HtmlLayout,PatternLayout等),但没有一个起作用。谷歌搜索几乎没有用处,因为唯一的相关结果是this question with no answers or comments(也在以前的Log4j版本上)和this unresolved issue with no comments(看起来像我面临的问题)。使用RollingFile和控制台附加程序记录事件的效果很好。
这是log4j2.xml的相关部分(电子邮件和SMTP服务器已更改为示例邮件):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<SMTP>
<name>Mailer</name>
<subject>Error Log</subject>
<to>email@example.com</to>
<from>log4j2@example.com</from>
<smtpHost>smtp.example.com</smtpHost>
<smtpPort>25</smtpPort>
<ignoreExceptions>false</ignoreExceptions>
<smtpProtocol>smtp</smtpProtocol>
<smtpDebug>true</smtpDebug>
<HtmlLayout charset="UTF-8" locationInfo="true"/>
</SMTP>
</Appenders>
<Loggers>
<Logger name="com.example" level="info">
<AppenderRef ref="Mailer"/>
</Logger>
</Loggers>
</Configuration>
SMTP调试输出不包含任何错误,并且对我来说似乎完全可以(除了缺少的DATA内容):
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: useEhlo true, useAuth false
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: trying to connect to host "smtp.example.com", port 25, isSSL false
May 02 13:36:21 example.com server[30644]: 220 smtp.example.com ESMTP Postfix (Debian/GNU)
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: connected to host "smtp.example.com", port: 25
May 02 13:36:21 example.com server[30644]: EHLO example.com
May 02 13:36:21 example.com server[30644]: 250-smtp.example.com
May 02 13:36:21 example.com server[30644]: 250-PIPELINING
May 02 13:36:21 example.com server[30644]: 250-SIZE 10240000
May 02 13:36:21 example.com server[30644]: 250-VRFY
May 02 13:36:21 example.com server[30644]: 250-ETRN
May 02 13:36:21 example.com server[30644]: 250-STARTTLS
May 02 13:36:21 example.com server[30644]: 250-ENHANCEDSTATUSCODES
May 02 13:36:21 example.com server[30644]: 250-8BITMIME
May 02 13:36:21 example.com server[30644]: 250-DSN
May 02 13:36:21 example.com server[30644]: 250 SMTPUTF8
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "PIPELINING", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "SIZE", arg "10240000"
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "VRFY", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "ETRN", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "STARTTLS", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "8BITMIME", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "DSN", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Found extension "SMTPUTF8", arg ""
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: use8bit false
May 02 13:36:21 example.com server[30644]: MAIL FROM:<log4j2@example.com>
May 02 13:36:21 example.com server[30644]: 250 2.1.0 Ok
May 02 13:36:21 example.com server[30644]: RCPT TO:<email@example.com>
May 02 13:36:21 example.com server[30644]: 250 2.1.5 Ok
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: Verified Addresses
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: email@example.com
May 02 13:36:21 example.com server[30644]: DATA
May 02 13:36:21 example.com server[30644]: 354 End data with <CR><LF>.<CR><LF>
May 02 13:36:21 example.com server[30644]: Date: Thu, 2 May 2019 13:36:21 +0200 (CEST)
May 02 13:36:21 example.com server[30644]: From: log4j2@example.com
May 02 13:36:21 example.com server[30644]: To: email@example.com
May 02 13:36:21 example.com server[30644]: Message-ID: <1720516454.3.1556792301896@example.com>
May 02 13:36:21 example.com server[30644]: Subject: Error Log
May 02 13:36:21 example.com server[30644]: MIME-Version: 1.0
May 02 13:36:21 example.com server[30644]: Content-Type: multipart/mixed;
May 02 13:36:21 example.com server[30644]: boundary="----=_Part_2_1938514944.1556792301895"
May 02 13:36:21 example.com server[30644]: .
May 02 13:36:21 example.com server[30644]: 250 2.0.0 Ok: queued as E23DBBEE73
May 02 13:36:21 example.com server[30644]: DEBUG SMTP: message successfully delivered to mail server
May 02 13:36:21 example.com server[30644]: QUIT
May 02 13:36:21 example.com server[30644]: 221 2.0.0 Bye
我希望电子邮件正文至少包含该错误,如果不是以前的事件,则因为SMTP附加程序显然对此具有循环缓冲区。