<style>
/* Changing the layout to use less space for mobiles */
@media screen and (max-device-width: 480px), screen and (-webkit-min-device-pixel-ratio: 2) {
#email-body { min-width: 30em !important; }
#email-page { padding: 8px !important; }
#email-banner { padding: 8px 8px 0 8px !important; }
#email-avatar { margin: 1px 8px 8px 0 !important; padding: 0 !important; }
#email-fields { padding: 0 8px 8px 8px !important; }
#email-gutter { width: 0 !important; }
}
</style>
<div id="email-body">
<table id="email-wrap" align="center" border="0" cellpadding="0" cellspacing="0" style="background-color:#f0f0f0;color:#000000;width:100%;">
<tr valign="top">
<td id="email-page" style="padding:16px !important;">
<table align="center" border="0" cellpadding="0" cellspacing="0" style="background-color:#ffffff;border:1px solid #bbbbbb;color:#000000;width:100%;">
<tr valign="top">
<td bgcolor="#003366" style="background-color:#003366;color:#ffffff;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;line-height:1;"><img src="http://jira.qos.ch/s/en_USb9v8he-418945332/850/25/_/jira-logo-scaled.png" alt="" style="vertical-align:top;" /></td>
</tr><tr valign="top">
<td id="email-banner" style="padding:32px 32px 0 32px;">
<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="width:100%;">
<tr valign="top">
<td style="color:#505050;font-family:Arial,FreeSans,Helvetica,sans-serif;padding:0;">
<img id="email-avatar" src="http://jira.qos.ch/secure/useravatar?avatarId=10122" alt="" height="48" width="48" border="0" align="left" style="padding:0;margin: 0 16px 16px 0;" />
<div id="email-action" style="padding: 0 0 8px 0;font-size:12px;line-height:18px;">
<a class="user-hover" rel="binkley" id="email_binkley" href="http://jira.qos.ch/secure/ViewProfile.jspa?name=binkley" style="color:#326ca6;">B. K. Oxley (binkley)</a>
commented on <img src="http://jira.qos.ch/images/icons/issuetypes/newfeature.png" height="16" width="16" border="0" align="absmiddle" alt="New Feature"> <a style='color:#326ca6;text-decoration:none;' href='http://jira.qos.ch/browse/LOGBACK-939'>LOGBACK-939</a>
</div>
<div id="email-summary" style="font-size:16px;line-height:20px;padding:2px 0 16px 0;">
<a style='color:#326ca6;text-decoration:none;' href='http://jira.qos.ch/browse/LOGBACK-939'><strong>add %match(...) converter for pattern layout</strong></a>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr valign="top">
<td id="email-fields" style="padding:0 32px 32px 32px;">
<table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
<tr valign="top">
<td id="email-gutter" style="width:64px;white-space:nowrap;"></td>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr valign="top">
<td colspan="2" style="color:#000000;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:12px;padding:0 0 16px 0;width:100%;">
<div class="comment-block" style="background-color:#edf5ff;border:1px solid #dddddd;color:#000000;padding:12px;"><p>Repasting code with markup (cannot edit the JIRA I created!):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">import</span> ch.qos.logback.classic.PatternLayout;
<span class="code-keyword">import</span> ch.qos.logback.classic.pattern.ClassicConverter;
<span class="code-keyword">import</span> ch.qos.logback.classic.spi.ILoggingEvent;
<span class="code-keyword">import</span> ch.qos.logback.core.boolex.EvaluationException;
<span class="code-keyword">import</span> ch.qos.logback.core.boolex.EventEvaluator;
<span class="code-keyword">import</span> ch.qos.logback.core.status.ErrorStatus;
<span class="code-keyword">import</span> javax.annotation.Nonnull;
<span class="code-keyword">import</span> java.util.LinkedHashMap;
<span class="code-keyword">import</span> java.util.List;
<span class="code-keyword">import</span> java.util.Map;
<span class="code-keyword">import</span> java.util.Objects;
<span class="code-keyword">import</span> <span class="code-keyword">static</span> ch.qos.logback.core.CoreConstants.EVALUATOR_MAP;
<span class="code-keyword">import</span> <span class="code-keyword">static</span> java.util.Collections.emptyMap;
/**
* {@code MarkedConverter} provides alternate conversions based on conditions. Enable with:
* <pre>
* <conversionRule
* conversionWord=<span class="code-quote">"match"</span>
* converterClass=<span class="code-quote">"mbl.laboratory.util.logging.osi.MatchConverter"</span>/></pre> Use as:
* <pre>
* <pattern>%match{cond1,patt1,...,fallback}</pattern></pre> Example:
* <pre>
* <evaluator name=<span class="code-quote">"WITH_MARKER"</span>>
* <expression><span class="code-keyword">null</span> != marker &mp;&mp; <span class="code-quote">"ALERT"</span>.equals(marker.getName())</expression>
* </evaluator>
* <pattern>%match(WITH_MARKER,%marker/%level,%level)</pattern></pre> will log
* <span class="code-quote">"ALERT/ERROR"</span> when marker is <span class="code-quote">"ALERT"</span> and level is <span class="code-quote">"ERROR"</span>, otherwise just <span class="code-quote">"ERROR"</span>.
*
* @author <a href=<span class="code-quote">"mailto:binkley@alumni.rice.edu"</span>>B. K. Oxley (binkley)</a>
* @todo Fix error reporting - logback swallows
*/
<span class="code-keyword">public</span> <span class="code-keyword">final</span> class MatchConverter
<span class="code-keyword">extends</span> ClassicConverter {
<span class="code-keyword">private</span> <span class="code-keyword">static</span> <span class="code-keyword">final</span> <span class="code-object">int</span> MAX_ERROR_COUNT = 4;
<span class="code-keyword">private</span> Map<<span class="code-object">String</span>, <span class="code-object">String</span>> conditions;
<span class="code-keyword">private</span> <span class="code-object">String</span> unmatched;
<span class="code-keyword">private</span> Map<<span class="code-object">String</span>, EventEvaluator<ILoggingEvent>> evaluators;
<span class="code-keyword">private</span> <span class="code-object">int</span> errors;
@Override
<span class="code-keyword">public</span> void start() {
<span class="code-keyword">final</span> List<<span class="code-object">String</span>> options = getOptionList();
<span class="code-keyword">if</span> (<span class="code-keyword">null</span> == options || 2 > options.size()) {
addError(<span class="code-quote">"Missing options <span class="code-keyword">for</span> %match - "</span> + Objects.toString(options));
conditions = emptyMap();
unmatched = "";
<span class="code-keyword">return</span>;
}
conditions = <span class="code-keyword">new</span> LinkedHashMap<>();
<span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i < options.size() - 1; i += 2)
conditions.put(options.get(i), options.get(i + 1));
unmatched = 0 == options.size() % 2 ? "" : options.get(options.size() - 1);
evaluators = (Map<<span class="code-object">String</span>, EventEvaluator<ILoggingEvent>>) getContext()
.getObject(EVALUATOR_MAP);
<span class="code-keyword">super</span>.start();
}
@Nonnull
@Override
<span class="code-keyword">public</span> <span class="code-object">String</span> convert(@Nonnull <span class="code-keyword">final</span> ILoggingEvent event) {
<span class="code-keyword">for</span> (<span class="code-keyword">final</span> Map.Entry<<span class="code-object">String</span>, <span class="code-object">String</span>> entry : conditions.entrySet())
<span class="code-keyword">if</span> (evaluate(entry.getKey(), event))
<span class="code-keyword">return</span> relayout(entry.getValue(), event);
<span class="code-keyword">return</span> relayout(unmatched, event);
}
<span class="code-keyword">private</span> <span class="code-object">boolean</span> evaluate(<span class="code-keyword">final</span> <span class="code-object">String</span> name, <span class="code-keyword">final</span> ILoggingEvent event) {
<span class="code-keyword">final</span> EventEvaluator<ILoggingEvent> evaluator = evaluators.get(name);
<span class="code-keyword">try</span> {
<span class="code-keyword">return</span> <span class="code-keyword">null</span> != evaluator && evaluator.evaluate(event);
} <span class="code-keyword">catch</span> (<span class="code-keyword">final</span> EvaluationException e) {
errors++;
<span class="code-keyword">if</span> (errors < MAX_ERROR_COUNT) {
addError(<span class="code-quote">"Exception thrown <span class="code-keyword">for</span> evaluator named ["</span> + evaluator.getName() + <span class="code-quote">"]"</span>, e);
} <span class="code-keyword">else</span> <span class="code-keyword">if</span> (errors == MAX_ERROR_COUNT) {
ErrorStatus errorStatus = <span class="code-keyword">new</span> ErrorStatus(
<span class="code-quote">"Exception thrown <span class="code-keyword">for</span> evaluator named ["</span> + evaluator.getName() + <span class="code-quote">"]."</span>, <span class="code-keyword">this</span>,
e);
errorStatus.add(<span class="code-keyword">new</span> ErrorStatus(
<span class="code-quote">"This was the last warning about <span class="code-keyword">this</span> evaluator's errors."</span>
+ <span class="code-quote">"We don't want the StatusManager to get flooded."</span>, <span class="code-keyword">this</span>));
addStatus(errorStatus);
}
<span class="code-keyword">return</span> <span class="code-keyword">false</span>;
}
}
<span class="code-keyword">private</span> <span class="code-object">String</span> relayout(<span class="code-keyword">final</span> <span class="code-object">String</span> pattern, <span class="code-keyword">final</span> ILoggingEvent event) {
<span class="code-keyword">final</span> PatternLayout layout = <span class="code-keyword">new</span> PatternLayout();
layout.setContext(getContext());
layout.setPattern(pattern);
layout.start();
<span class="code-keyword">return</span> layout.doLayout(event);
}
}
</pre>
</div></div></div>
<div style="color:#505050;padding:4px 0 0 0;"> </div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td><!-- End #email-page -->
</tr>
<tr valign="top">
<td style="color:#505050;font-family:Arial,FreeSans,Helvetica,sans-serif;font-size:10px;line-height:14px;padding: 0 16px 16px 16px;text-align:center;">
This message is automatically generated by JIRA.<br />
If you think it was sent incorrectly, please contact your JIRA administrators<br />
For more information on JIRA, see: <a style='color:#326ca6;' href='http://www.atlassian.com/software/jira'>http://www.atlassian.com/software/jira</a>
</td>
</tr>
</table><!-- End #email-wrap -->
</div><!-- End #email-body -->