重新登录SMTPAppender仅发送最后一封电子邮件 [英] Logback SMTPAppender only sending last email

查看:132
本文介绍了重新登录SMTPAppender仅发送最后一封电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将LogbackSMTPAppender一起用作我的日志记录解决方案.我在appender上附加了一个评估程序,该评估程序每记录100个错误就会触发一封电子邮件的发送.在大多数情况下,这可以正常工作,但是我注意到,如果我在循环中快速创建错误以触发多封电子邮件,则只会发送最后一封包含正确内容的电子邮件.这似乎是一种竞争状况,其中第一封电子邮件尚未完成创建/发送,而第二封电子邮件在创建时会覆盖第一封电子邮件.有没有其他人经历过或找到解决方案.我的登录配置位于下面.

I am attempting to use Logback with SMTPAppender as my logging solution. I have an evaluator attached to the appender that triggers an email to be sent every 100 errors logged. For the most part this works correctly, but I've noticed if I create errors rapidly in a loop to trigger multiple emails, only the last email gets sent, with the correct content. It seems to be a race condition where the first email is not finished being created/sent, and the second one overwrites the first on creation. Has anyone else experienced this or found a solution. My logback config is attached below.

<configuration>
    <appender name="emailAppender" class="${logback.emailAppenderClass}">
       <evaluator class="com.wdp.common.logging.logback.evaluators.CountingLoggerEvaulator">
            <limit>100</limit>
        </evaluator>
        <to>${logback.emailNotificationRecipientStr}</to>
        <from>${logback.emailNotificationFromStr}</from>
        <smtpHost>${logback.smtpHost}</smtpHost>
        <subject>Logback logs for facebook-ads-processes</subject>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d [Thread:%t] %p [%c] - %m%n</pattern>
        </layout>  

            <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
            <bufferSize>${logback.cyclicBufferSize}</bufferSize>
        </cyclicBufferTracker>
    </appender>
</configuration>

这是评估者:

public class CountingLoggerEvaulator extends EventEvaluatorBase<ILoggingEvent> implements EventEvaluator<ILoggingEvent> {
    //if limit is not set in configuration, this will cause it to send one email for each message received.
    private int limit = 100;
    private int counter = 0;

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getLimit() {
        return limit;
    }

    @Override
    public boolean evaluate(ILoggingEvent expr) throws NullPointerException, EvaluationException {
        counter++;
        if (counter == limit) {
            counter = 0;
            return true;
        } else {
            return false;
        }
    }
}

非常感谢您的帮助.

推荐答案

问题是SMTPAppender的异步发送属性默认为true.我将其设置为false并可以正常运行

The issue was the asynchronousSending attribute of the SMTPAppender defaults to true. I set it to false and it works properly

这篇关于重新登录SMTPAppender仅发送最后一封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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