5

我正在创建一个管理页面,其中有几个我想即时设置的 logback 属性,其中一个是我向其发送系统警报的管理电子邮件。的 apiSMTPAppender具有添加到“到”地址列表或将它们作为列表获取的方法,但我没有找到任何要清除、删除或更新它们的方法。我该怎么做?

我目前看到两个选项:

  1. 一种选择是删除 appender 并使用新属性创建一个新的 (yuck)。
  2. 弄清楚如何直接通过 Joran 来配置它(也许很糟糕?)。

我正在推进(2),但如果有更好的方法,请发布。

4

3 回答 3

4

我能想到的另外两个选择:

1/ 将更新的地址设置为系统属性,并SMTPAppender使用系统属性配置以评估 smtpTo 地址,如下所示

<to>%property{smtpTo}</to> 

或 2/ 在数据库/系统属性的某处设置更新的地址。将 smtpTo 地址放入每个线程的 MDC,并配置为SMTPAppender从 MDC 获取它,如下所示

<to>%X{smtpTo}</to>
于 2012-07-30T07:21:04.480 回答
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);

希望它能有用。

于 2012-11-08T13:02:42.663 回答
4

我这样做:

smtpappender.getToList().clear();
于 2013-03-05T22:01:23.633 回答