हो सकता है कि आप इस पूरे कर चुके हैं की तरह MDC से इसे पाने के लिए है, लेकिन मैं सिर्फ गतिशील सेट करने के लिए "के लिए एक रास्ता खोजने के लिए की जरूरत "पते, और यह विषय मुझे रास्ते में ले जाता है (सिस्टम गुणों और एमडीसी मूल्यों पर @gresdiplitude विचार के लिए धन्यवाद), इसलिए मैं अपना समाधान साझा कर रहा हूं।
मैं छोटी निष्पादन रिपोर्ट भेजने के लिए 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 "[email protected], [email protected]"
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
"[email protected], [email protected]", // smtpTo
"alertGroup"); // discriminatingValue
logger.warn(marker1, "SNAFU");
Marker marker2 = new MyMarkerImpl(
"REPORT", "[email protected], [email protected]", "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", "[email protected], [email protected]", "bugFixingGroup");
logger.error(marker3, "Why things gone bad", exception);
आशा है कि यह उपयोगी हो सकता है।
यह बहुत अच्छा है।इस सुविधा के कार्यान्वयन को धक्का दिया गया था, यह सोचकर कि इसे करने का एक बेहतर तरीका अंततः लागू किया जाएगा, या पोस्ट के आसपास एक काम किया जाएगा। अभी के लिए, मुझे लगता है कि यह सबसे अच्छा तरीका है। हालांकि, मेरे पास एक enum है कि मैं उन सभी संग्रहीत अनुप्रयोगों में "जीवित" के साथ उपयोग करता हूं। मैं मार्करों को परिवर्तनीय बना दूंगा, लेकिन केवल एक उदाहरण होगा। – Noremac
@ नोरेमैक यह एक साधारण दृष्टिकोण है, हालांकि मैं किसी भी प्रकार के उत्परिवर्तनीय enums नापसंद करता हूं। यह आपको रेस कंडीशन के मुद्दों पर भी ले जा सकता है यदि आपका एप्लिकेशन मल्टीथ्रेड किया गया है, एक वेब एप्लिकेशन उपयोगकर्ता अनुरोध पर एसएमटीपी लॉगिंग फायरिंग, या क्लस्टर्ड वातावरण। मेरा तरीका आम तौर पर पृथक सरल मार्करों को लपेटने वाले अपरिवर्तनीय मार्कर बनाते हैं, और जब तक उनकी आवश्यकता होती है तब तक उन्हें संग्रहीत किया जाता है। यह जानना अच्छा है कि यह आपके लिए उपयोगी हो सकता है =) – mdrg