简体   繁体   English

Log4j2:SMTPAppender不发送错误或致命级别的邮件

[英]Log4j2: SMTPAppender does not send mails with error or fatal level

I recognized some problems with the SMTPAppender in log4j2. 我在log4j2中发现了SMTPAppender的一些问题。 Whenever log events with the level error or fatal are created without having an event with the level info before no mail is sent and the fatal event disappears. 每当与级别的日志事件, errorfatal,而不必与水平的情况下创建info之前没有邮件发送和致命事件消失。

Here is my log4j2 configuration file (log4j2.xml) and a small program (LogTest.java) to reproduce the problem: 这是我的log4j2配置文件(log4j2.xml)和一个重现问题的小程序(LogTest.java):

<?xml version="1.0" encoding="UTF-8"?>
    <configuration status="warn">

        <!-- mail server configuration -->
        <properties>
            <property name="receipients">me@example.com</property>
            <property name="from">me@example.com</property>
            <property name="smtpHost">smtp.example.com</property>
            <property name="smtpPort">25</property>
            <property name="smtpProtocol">smtp</property>
            <property name="smtpUser">me</property>
            <property name="smtpPassword">secret</property>
        </properties>

    <appenders>

        <!-- appender to write all info events to stdout -->
        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="info" onMatch="NEUTRAL" onMismatch="DENY"/>
        </Console>

        <!-- appender to send mails (default: error and fatal events)-->
        <SMTP name="Mailer" suppressExceptions="false"
              subject="Error log" to="${receipients}" from="${from}"
              smtpHost="${smtpHost}" smtpPort="${smtpPort}"
              smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}" 
              smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="2">
        </SMTP>
        <!-- appender to send mails asynchronously -->
        <Async name="AsyncMailer" > 
            <appender-ref ref="Mailer"/>
        </Async>

    </appenders>
    <loggers>

        <!-- logger to send mail on (at least) info level events -->
        <logger name="LogTest" level="info" additivity="true">
            <appender-ref ref="AsyncMailer"/>
        </logger>

        <!-- root logger to see what happens (info level and "above") -->
        <root level="info">
            <appender-ref ref="Console"/>
        </root>

    </loggers>
</configuration>

I used this small program to reproduce the problem (LogTest.java): 我使用这个小程序来重现问题(LogTest.java):

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

class LogTest
{
    private static Logger logger=LogManager.getLogger("LogTest");

    public void testlogger()
    {
        /* --> uncomment to enable first mail
        logger.info("test info 1");
        */
        logger.fatal("test fatal 1");

        /* --> uncomment to enable second mail
        logger.info("test info 2");
        */
        logger.fatal("test fatal 2");
    }

    public static void main(String[] args)
    {
        LogTest app=new LogTest();
        app.testlogger();
    }

}

If you uncomment the two marked positions everything work like intended: two mails are sent - each containing the fatal-event and the prior info event. 如果取消注释两个标记位置,一切都按预期工作:发送两封邮件 - 每封邮件包含致命事件和先前的信息事件。 Additionally the 4 events are printed to stdout: 此外,4个事件打印到stdout:

test info 1
test fatal 1
test info 2
test fatal 2

Now, if you only activate/uncomment the second position - the second mail (fatal2) is sent as intended (again with the prior info2 event), but even though the first fatal event is printed to stdout the mail is eaten up. 现在,如果您只激活/取消注释第二个位置 - 第二个邮件(fatal2)按预期发送(再次使用先前的info2事件),但即使第一个致命事件打印到stdout,邮件也会被吃掉。 The output looks as follows: 输出如下:

test fatal 1
test info 2
test fatal 2

Personally, for me it seems like I got something wrong and mis-configured log4j2 or it might be a bug. 就个人而言,对我而言,似乎我遇到了错误并且错误配置了log4j2,或者它可能是一个错误。

Thanks for your help in advance. 感谢您的帮助。

*Jost *约斯特

Note: 注意:

For the tests I used log4j2-beta7 downloaded from the project's website. 对于测试,我使用从项目网站下载的log4j2-beta7 The documentation can be found here . 文档可以在这里找到。

At first glance this looks like a bug. 乍一看,这看起来像一个bug。 Does it still happen if you remove the LogTest logger and configure your root logger like this? 如果您删除LogTest记录器并像这样配置根记录器,它仍然会发生吗?

<root level="info">
    <appender-ref ref="Console"/>
    <appender-ref ref="AsyncMailer"/>
</root>

FYI, if later you need different log levels on the different appenders you can achieve that like this (no need for a separate logger): 仅供参考,如果以后您需要不同的appender上的不同日志级别,您可以实现这样的目标(不需要单独的记录器):

<root level="trace">
    <appender-ref ref="A" level="info" />
    <appender-ref ref="B" level="debug" />
</root>

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

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