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

核能气质少年 提交于 2019-12-01 03:16:08

问题


I recognized some problems with the SMTPAppender in log4j2. 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.

Here is my log4j2 configuration file (log4j2.xml) and a small program (LogTest.java) to reproduce the problem:

<?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):

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:

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

Thanks for your help in advance.

*Jost

Note:

For the tests I used log4j2-beta7 downloaded from the project's website. The documentation can be found here.


回答1:


At first glance this looks like a bug. Does it still happen if you remove the LogTest logger and configure your root logger like this?

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

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


来源:https://stackoverflow.com/questions/17657983/log4j2-smtpappender-does-not-send-mails-with-error-or-fatal-level

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