概述
配置错误日志发送到指定邮箱
<appender name="EMAIL" class="com.ppdai.logclient.logback.MailAppender">
<evaluator class="com.ppdai.logback.SMTPFrequencyEvaluator">
<maxCount>1000</maxCount>
<expiredTime>300</expiredTime>
</evaluator>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<bufferSize>1</bufferSize>
</cyclicBufferTracker>
<smtpHost>smtp.awesome.com</smtpHost>
<smtpPort>25</smtpPort>
<username>[email protected]</username>
<password></password>
<to>[email protected]</to>
<from>[email protected]</from>
<subject>【cloud_iview报错邮件】%X{ip}: %logger{20} - %m 累积错误数:%X{error_count}
</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}%level%thread%logger{35}%message
</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
SMTPAppender为logback里面提供的API,继续该API的MailAppender源码如下:
public class MailAppender extends SMTPAppender {
public MailAppender() {
}
protected void append(ILoggingEvent leo) {
VariableLoggingEventVO eventVO = VariableLoggingEventVO.build(leo);
this.filter(eventVO);
super.append(eventVO);
}
private void filter(VariableLoggingEventVO eventVO) {
Map<String, String> newMdc = new HashMap(eventVO.getMdc());
eventVO.setMdcPropertyMap(newMdc);
eventVO.setFormattedMessage(Desensitiver.desensitive(eventVO.getFormattedMessage()));
}
}
public class Desensitiver {
public Desensitiver() {
}
public static String desensitive(String source) {
return PhoneDesensitiver.filterPhone(IdCardDesensitiver.filterIdCard(source));
}
}