Log4j2:SMTPAppender不发送错误或致命级别的邮件 [英] Log4j2: SMTPAppender does not send mails with error or fatal level

查看:142
本文介绍了Log4j2:SMTPAppender不发送错误或致命级别的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在log4j2中发现了SMTPAppender的一些问题。每当创建级别为错误致命的日志事件时没有的事件包含在没有邮件发送且致命事件消失之前,等级 info

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.

这是我的log4j2配置文件(log4j2。 xml)和一个小程序(LogTest.java)来重现问题:

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>






我用这个小程序重现问题( LogTest.java):


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();
    }

}






如果取消注释两个标记位置,一切都按预期工作:发送两封邮件 - 每封邮件包含致命事件和先前的信息事件。此外,4个事件打印到stdout:


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

现在,如果你只激活/取消注释第二个位置 - 第二个位置mail(fatal2)按预期发送(再次使用先前的info2事件),但即使第一个致命事件打印到stdout,邮件也会被吃掉。输出如下:

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






就我个人而言,似乎我有些不对劲和错误-configured log4j2或者它可能是一个bug。


Personally, for me it seems like I got something wrong and mis-configured log4j2 or it might be a bug.

感谢你的帮助。

* Jost

注意:

对于我使用的测试 log4j2-beta7
文档可以在这里找到

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

推荐答案

乍一看,这看起来像个错误。如果您删除LogTest记录器并配置这样的根记录器,它仍然会发生吗?

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>

仅供参考,如果以后您需要在不同的appender上使用不同的日志级别,您可以实现这一目标(不需要一个单独的记录器):

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>

这篇关于Log4j2:SMTPAppender不发送错误或致命级别的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆