Whisper 是控制异常邮件变成垃圾邮件的一个工具。Whisper 作为一个过滤的 appender 来代替默认的 SMTP 邮件 appender。当消息的频率超过配置的阈值的时候,Whisper 就会开始压制。当启动压制,Whisper 就会发送一定时期的数据,让你知道哪些信息被压制,有多少信息被压制。当问题解决,你的错误信息频率开始下降,那么就会结束压制,一切恢复如常。所有的这些都基于一个日志信息基础。
whisper-support)Whisper 支持哪些日志记录框架?
Whisper 当前支持 Logback,不就将会支持 log4j 和 log4jv2。
Whisper 现已提供在 Maven Central:
<groupId>com.eclecticlogic</groupId> <artifactId>whisper</artifactId> <packaging>jar</packaging> <version>1.0.0</version>
Whisper?
把 whisper-logback-sample.xml 放在 src/sample/resources。
配置 Whisper appender 之前要配置其他的两个 appenders:ERROR 错误级别日志的正则邮件 appender 和第二个发送 suppression Digests 的邮件 appender:
<appender name="errorEmail" class="ch.qos.logback.classic.net.SMTPAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <smtpHost>ADDRESS-OF-YOUR-SMTP-HOST</smtpHost> <to>EMAIL-DESTINATION</to> <from>SENDER-EMAIL</from> <subject>TESTING: %logger{20} - %m</subject> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date %-5level %logger{35} - %message%n</pattern> </layout> </appender> <appender name="errorDigest" class="ch.qos.logback.classic.net.SMTPAppender"> <smtpHost>ADDRESS-OF-YOUR-SMTP-HOST</smtpHost> <to>EMAIL-DESTINATION</to> <from>SENDER-EMAIL</from> <subject>%X{whisper.digest.subject}</subject> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date %-5level %logger{35} - %message%n</pattern> </layout> </appender>
注意 %X{whisper.digest.subject} 的使用。
%X{whisper.digest.subject}
配置 Whisper appender:
<appender name="whisper" class="com.eclecticlogic.whisper.logback.WhisperAppender"> <!-- Filter out non error logs --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <!-- This is the name of the logging category to use to send out error digests. This is associated with the errorDigest appender. --> <digestLoggerName>digest.appender.logger</digestLoggerName> <!-- suppressAfter specifies the criteria to enter suppression. The example below says that if 3 errors of the same kind are encountered within a 5 minute window, then suppression should kick in. --> <suppressAfter>3 in 5 minutes</suppressAfter> <!-- expireAfter specifies how much of silence the logger between messages before stopping suppression. --> <expireAfter>4 minutes</expireAfter> <!-- digestFrequency specifies how often error email digests should be sent containing statistics on messages suppressed --> <digestFrequency>20 minutes</digestFrequency> <!-- The pass-through appender for the normal case when suppression is not in-force. --> <appender-ref ref="errorEmail" /> </appender>
数字日志记录名字关联 digestAppender,默认的 appenders 列表包括 whisper appender:
<logger name="digest.appender.logger" level="error" additivity="false"> <appender-ref ref="errorDigest" /> </logger> <root level="debug"> <appender-ref ref="whisper" /> <appender-ref ref="fileAppender" /> </root>