本文介绍了log4j2 SMTP Appender:如何将以前的消息包含在另一个级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用log4j2-beta9,我有以下配置(部分内容):

I'm using log4j2-beta9 and I have the following config (part of it):

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

使用此配置,我只收到1(一)个错误消息的电子邮件。
如何配置log4j2以接收1条错误消息和N条之前的
消息,LEVEL = INFO?

With this configuration I receive email with just 1 (one) error message.How can I configure log4j2 to receive 1 error message and N previousmessages with LEVEL=INFO?

提前致谢。

推荐答案

目前无法做到这一点:SMTP appender有,它在通过电子邮件发送之前捕获日志事件,但它只捕获为目标级别配置的事件(示例中为ERROR) )。如果您将其更改为INFO,您将收到所有INFO级别日志消息的电子邮件通知(而不仅仅是ERROR级别消息之前的消息)。

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

您可以提高这是log4j 或log4j用户邮件列表中的功能请求。

You can raise this as a feature request on the log4j issue tracker or log4j-user mailing list.

更正:我错误,错误,错误

STMP Appender确实有一个缓冲区,但它意味着捕获ERROR日志事件之前的最后一个X(默认为512)INFO,DEBUG,TRACE级别的消息。所以它应该像你期望的那样工作(就像log4j-1.x SMTP appender一样)。

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. So it is supposed to work like you expected (like the log4j-1.x SMTP appender works).

SMTP Appender会在收到错误时触发一封电子邮件(或更严重的)级别日志事件。所以在你的配置中你应该只向这个appender发送ERROR级别的日志事件(或者你会错过它之前的INFO,DEBUG,TRACE事件)。

The SMTP Appender will fire an email when it gets an ERROR (or more severe)-level log event. 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).

在您的配置中,将< AppenderRef ref =AsyncMailerlevel =error/> 更改为< AppenderRef ref =AsyncMailer/>

In your config, change <AppenderRef ref="AsyncMailer" level="error"/> to <AppenderRef ref="AsyncMailer"/>.

这应解决问题。如果您仍然遇到问题,其他人类似问题,并且显然找到了解决方法通过在配置中添加ThresholdFilter:

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:

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

这篇关于log4j2 SMTP Appender:如何将以前的消息包含在另一个级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:08