gpt4 book ai didi

java - 如何更新 Logback 中 SMTPAppender 上的 "to"地址?

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:39 32 4
gpt4 key购买 nike

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

我目前看到两个选项:

  1. 一种选择是删除附加程序并创建一个具有新属性的新附加程序(糟糕)。
  2. 弄清楚如何直接通过 Joran 进行配置(也许很糟糕?)。

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

最佳答案

也许你已经完成了这个,但我只需要找到一种方法来设置动态“到”地址,这个主题引导我找到了方法(感谢@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);

希望对你有用。

关于java - 如何更新 Logback 中 SMTPAppender 上的 "to"地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11636680/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com