我正在创建一个管理页面,其中包含一些我想要动态设置的logback属性,其中一个是我发送系统警报的管理员电子邮件。 SMTPAppender
的api具有添加到“to”地址列表的方法,或者将它们作为列表获取,但我没有找到任何要清除,删除或更新它们的内容。我该怎么做?
我目前看到两个选项:
我正在向前推进(2),但如果有更好的方法,请发帖。
答案 0 :(得分:4)
我能想到的另外两个选择:
1 /将更新的地址设置为系统属性,并使用系统属性配置SMTPAppender
以评估smtpTo地址
<to>%property{smtpTo}</to>
或者
2 /在某处设置更新的地址数据库/系统属性。将smtpTo地址放入每个线程的MDC中,并配置SMTPAppender
以便从MDC获取它,如此
<to>%X{smtpTo}</to>
答案 1 :(得分:4)
也许你已经完成了这个,但我只需要找到一种方法来设置动态“到”地址,这个主题引导我走向(感谢@gresdiplitude关于系统属性和MDC值的想法),所以我正在分享我的解决方案。
我正在使用SMTPAppender
发送小型执行报告,但这些报告需要发送到不同的邮箱。该解决方案看起来很简单,或者至少我对结果非常满意。
我的logback.xml:
<!-- this is the trick: a converter to use on the "to" field pattern -->
<conversionRule conversionWord="smtpTo" converterClass="com.example.logback.MyConverter" />
<appender name="SMTP" class="ch.qos.logback.classic.net.SMTPAppender">
<!-- a filter to select just the log entries that will be sent by mail -->
<filter class="com.example.logback.MyFilter" />
<!-- an evaluator, mine is like CounterBasedEvaluator from the manual:
http://logback.qos.ch/xref/chapters/appenders/mail/CounterBasedEvaluator.html
-->
<evaluator class="com.example.logback.MyEvaluator">
<limit>25</limit>
</evaluator>
<!-- a discriminator to create a cyclic buffer for each mailing group I need -->
<discriminator class="com.example.logback.MyDiscriminator" />
<!-- just matching buffer size to evaluator limit -->
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTrackerImpl">
<bufferSize>25</bufferSize>
</cyclicBufferTracker>
<smtpHost>${smtp.host}</smtpHost>
<smtpPort>${smtp.port}</smtpPort>
<SSL>${smtp.ssl}</SSL>
<username>${smtp.username}</username>
<password>${smtp.password}</password>
<from>${smtp.from}</from>
<!-- here you use the converter: in this case will get data
from marker containing the destination addresses
-->
<to>%smtpTo</to>
<subject>my subject</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date: %message%n%xThrowable{full}</pattern>
</layout>
</appender>
MyFilter.java:
public FilterReply decide(ILoggingEvent event) {
return event.getMarker() != null
&& event.getMarker().contains("REPORT") ? FilterReply.ACCEPT
: FilterReply.DENY;
}
MyDiscriminator.java:
public String getDiscriminatingValue(ILoggingEvent e) {
Marker marker = e.getMarker();
if (marker == null || !(marker instanceof MyMarker)) {
return null;
}
return ((MyMarker) marker).getDiscriminatingValue();
}
MyConverter.java:
public class MyConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
Marker marker = event.getMarker();
if (marker == null || !(marker instanceof MyMarker)) {
return null;
}
return ((MyMarker) marker).getSmtpTo();
}
}
MyMarker.java:
public interface MyMarker extends Marker {
// a list of destination addresses, like "a@c.com, x@y.net"
String getSmtpTo();
// an "id" to tell the buffers apart, could be "smtpTo" itself
// but in my case it would mix different reports that goes to the same addresses
String getDiscriminatingValue();
}
我刚刚为MyMarker
创建了一个实现,并在每个应该报告的日志语句中使用了它的几个实例:
// suggestion: make the marker immutable, then you can store and reuse them instead of recreating them every time
Marker marker1 = new MyMarkerImpl(
"REPORT", // marker name
"me@company.com, joe@company.com", // smtpTo
"alertGroup"); // discriminatingValue
logger.warn(marker1, "SNAFU");
Marker marker2 = new MyMarkerImpl(
"REPORT", "boss@company.com, ceo@company.com", "phbGroup");
logger.info(marker2, "Everything is fine");
// here we have same smtpTo as above but different discriminatingValues, so this will be sent in another email/report
Marker marker3 = new MyMarkerImpl(
"REPORT", "me@company.com, joe@company.com", "bugFixingGroup");
logger.error(marker3, "Why things gone bad", exception);
希望它有用。
答案 2 :(得分:4)
我这样做:
smtpappender.getToList().clear();