有没有办法在LogBack中配置SMTPAppender
以满足以下条件?
我目前的实现远不是上面做的,但是当发生异常时它会发送3封电子邮件 - 异常消息,堆栈跟踪和缓冲区刷新。
<!-- Filter duplicate Log Messages - Very important for Email Reports -->
<turboFilter class="ch.qos.logback.classic.turbo.DuplicateMessageFilter">
<AllowedRepetitions>1</AllowedRepetitions>
<CacheSize>1000</CacheSize>
</turboFilter>
<!--
############################################################
BASIC APPENDER
############################################################
-->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} %-55(%X{user} %level [%thread] %logger{20}) - %msg%n</pattern>
</encoder>
</appender>
<!--
############################################################
EMAIL APPENDER
############################################################
-->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<appender name="Email" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>SERVER</smtpHost>
<smtpPort>PORT</smtpPort>
<asynchronousSending>false</asynchronousSending>
<from>SENDER</from>
<to>RECIPIENT</to>
<subject>SUBJECT</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} %-55(%X{user} %level [%thread] %logger{20}) - %msg%n</pattern>
</layout>
</appender>
<!--
############################################################
OTHER
############################################################
-->
<root level="INFO">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="Email"/>
</root>
答案 0 :(得分:11)
一个简单的解决方案是将这些错误记录到文件中,并在服务器/计算机上安装一个脚本,该脚本每天读取一次文件并发送电子邮件。
如果你想使用一个appender,在我看来你需要自己动手,因为我不认为标准SMTPAppender
使你能够每天发送一次电子邮件:
SMTPAppender
sendBuffer
中的SMTPAppenderBase
方法,以便只将日志消息添加到集合中ScheduledExecutorService
,每天运行一次sendEmail
方法sendEmail
方法将在this
上同步以确保线程安全,检查集合是否为空,发送包含所有错误的电子邮件并清除集合基本实现可能看起来像下面的类(我没有测试过它 - 我使用的是Java 8语法,但如果需要,可以用匿名类替换它)。请注意,我只保留导致异常的事件,您可能还希望在sendBuffer方法中保留CyclicBuffer的内容和/或在sendEmail方法中的错误之间添加一些错误分隔符。这可能变得非常复杂,需要根据您的要求进行微调。
public class ScheduledSMTPAppender extends SMTPAppender {
private final ThreadFactory tf = r -> {
Thread t = new Thread(r, "ScheduledSMTPAppender Thread");
t.setDaemon(true); //make daemon or it will prevent your program to exit
return t;
};
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, tf);
private final List<ILoggingEvent> events = new ArrayList<> ();
private int maxMessages = 10;
public ScheduledSMTPAppender() { super(); }
public ScheduledSMTPAppender(EventEvaluator<ILoggingEvent> eventEvaluator) { super(eventEvaluator); }
@Override public void start() {
super.start();
scheduler.scheduleAtFixedRate(this::sendEmail, 1, 1, TimeUnit.DAYS);
}
@Override protected void sendBuffer(CyclicBuffer<ILoggingEvent> cb, ILoggingEvent lastEventObject) {
events.add(lastEventObject);
if (events.size() > maxMessages) sendEmail();
}
//needs to be synchronized for thread safety
private synchronized void sendEmail() {
try {
if (events.isEmpty()) return;
ILoggingEvent lastEvent = events.get(events.size() - 1);
events.remove(events.size() - 1);
CyclicBuffer<ILoggingEvent> cb;
if (events.isEmpty()) {
cb = new CyclicBuffer<>(1);
} else {
cb = new CyclicBuffer<>(events.size());
for (ILoggingEvent e : events) cb.add(e);
}
super.sendBuffer(cb, lastEvent);
events.clear();
} catch (Exception e) {
//Important to have a catch all here or the scheduled task will die
addError("Error occurred while sending e-mail notification.", e);
}
}
//this allows to make "maxMessages" a parameter of your appender
public int getMaxMessages() { return maxMessages; }
public void setMaxMessages(int maxMessages) { this.maxMessages = maxMessages; }
}
您的logback配置文件如下所示:
<appender name="Email" class="your.package.ScheduledSMTPAppender">
<smtpHost>SERVER</smtpHost>
<smtpPort>PORT</smtpPort>
<asynchronousSending>false</asynchronousSending>
<from>SENDER</from>
<to>RECIPIENT</to>
<subject>SUBJECT</subject>
<maxMessages>10</maxMessages>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} %-55(%X{user} %level [%thread] %logger{20}) - %msg%n</pattern>
</layout>
</appender>
为了更进一步,您可以添加参数,例如发送日期的时间,每天的电子邮件数量等。