简体   繁体   English

log4j2 SMTP Appender:如何将以前的消息包含在另一个级别?

[英]log4j2 SMTP Appender: How to include previous messages with another level?

I'm using log4j2-beta9 and I have the following config (part of it): 我正在使用log4j2-beta9,我有以下配置(部分):

<Appenders>
    <SMTP name="Mailer" suppressExceptions="false"
          subject="${subject}" to="${receipients}" from="${from}"
          smtpHost="${smtpHost}" smtpPort="${smtpPort}"
          smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
          smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="20">
        <PatternLayout>
            <pattern>%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %m%n</pattern>
        </PatternLayout>
    </SMTP>

    <Async name="AsyncMailer">
        <AppenderRef ref="Mailer"/>
    </Async>
</Appenders>
<Loggers>
    <Root level="info">
        <AppenderRef ref="AsyncMailer" level="error"/>
    </Root>
</Loggers>

With this configuration I receive email with just 1 (one) error message. 使用此配置,我只收到1(一)错误消息的电子邮件。 How can I configure log4j2 to receive 1 error message and N previous messages with LEVEL=INFO? 如何配置log4j2以接收1条错误消息和N条以前的LEVEL = INFO消息?

Thanks in advance. 提前致谢。

This is currently not possible: the SMTP appender has a buffer where it captures log events before emailing them, but it will only capture the events that are configured for the target level (ERROR in your example). 目前这是不可能的:SMTP appender有一个缓冲区 ,它在通过电子邮件发送之前捕获日志事件,但它只捕获为目标级别配置的事件(在您的示例中为ERROR)。 If you changed this to INFO, you would get email notifications for all INFO-level log messages (not just the ones preceding an ERROR-level message). 如果您将其更改为INFO,您将收到所有INFO级别日志消息的电子邮件通知(而不仅仅是ERROR级别消息之前的消息)。

You can raise this as a feature request on the log4j issue tracker or log4j-user mailing list. 您可以在log4j 问题跟踪器或log4j用户邮件列表中将其作为功能请求提出。


Correction: I was wrong, wrong, WRONG ! 更正:我错了,错了,错了

STMP Appender does have a buffer but it is meant to capture the last X (512 by default) INFO, DEBUG, TRACE-level messages that preceded the ERROR log event. STMP Appender确实有一个缓冲区,但它意味着捕获ERROR日志事件之前的最后一个X(默认为512)INFO,DEBUG,TRACE级别的消息。 So it is supposed to work like you expected (like the log4j-1.x SMTP appender works). 所以它应该像你期望的那样工作(就像log4j-1.x SMTP appender一样)。

The SMTP Appender will fire an email when it gets an ERROR (or more severe)-level log event. SMTP Appender将在收到ERROR(或更严重)级别的日志事件时触发电子邮件。 So in your config you should not only send ERROR-level log events to this appender (or you'll miss the INFO, DEBUG, TRACE events that precede it). 所以,在你的配置,你应该只发送错误级别的日志事件,该附加器(或你会错过它之前的信息,调试,跟踪事件)。

In your config, change <AppenderRef ref="AsyncMailer" level="error"/> to <AppenderRef ref="AsyncMailer"/> . 在您的配置中,将<AppenderRef ref="AsyncMailer" level="error"/>更改为<AppenderRef ref="AsyncMailer"/>

That should fix the issue. 这应该解决问题。 If you still experience problems, someone else reported a similar issue and apparently found a workaround by adding a ThresholdFilter to the configuration: 如果您仍然遇到问题,其他人报告了类似问题,并且显然通过在配置中添加ThresholdFilter找到了解决方法:

<Appenders>
    <SMTP name="Mailer" suppressExceptions="false"
          subject="${subject}" to="${receipients}" from="${from}"
          smtpHost="${smtpHost}" smtpPort="${smtpPort}"
          smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
          smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="20">

        <ThresholdFilter level="debug" onMatch="NEUTRAL" onMismatch="DENY" /> 
        <PatternLayout>
            <pattern>%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %m%n</pattern>
        </PatternLayout>
    </SMTP>

    <Async name="AsyncMailer">
        <AppenderRef ref="Mailer"/>
    </Async>
</Appenders>
<Loggers>
    <Root level="info">
        <AppenderRef ref="AsyncMailer" />
    </Root>
</Loggers>

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

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