Thursday, March 12, 2015

Neutral Filters and Evaluator ordering for Logback SMTPAppender

Example of config with nuance of NEUTRAL usage for filters,
miss of at least one filter that do deny by well distinctive parameter (level, marker) will result in sending mail with all events from begging of logs till event required in Evaluator.
:

<appender name="MY_MAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>mailer.server.com</smtpHost>
<to>botlog@server.com</to>
<from>noreply@server.com</from>

<asynchronousSending>false</asynchronousSending>

<if condition="property(&quot;MY_ENV_VAR&quot;) == &quot;&quot; || property(&quot;MY_ENV_VAR&quot;) == null">
<then>
<subject>[%marker] [%p] from ${HOSTNAME}</subject>
</then>
<else>
<subject>[${MY_ENV_VAR}] [%marker] [%p] from ${HOSTNAME}</subject>
</else>
</if>

<!-- the way SMTPAppender works makes him only apply filters firsly 
    and then apply evaluator to actually trigger 
e-mail sending, filters are also executed in order in 
which they are defined in configuration -->
<!-- Due to usage of NEUTRAL at EvaluatorFilter(see below) without 
    this filter, mail will contain all events from beggining 
    till MY_MARKER -->
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>MY_MARKER</marker>
</evaluator>
<OnMatch>NEUTRAL</OnMatch>
<OnMismatch>DENY</OnMismatch>
</filter>

<!-- perform more precise filtering, 
    you can suppress particular messages here -->
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression><![CDATA[
// reduce noise of job for some problems
if (level == WARN && logger.contains("ConstraintService") 
&& message.contains("violates constraint")) {
return false;
}
return true;
]]></expression>
</evaluator>
<!-- NEUTRAL as we may want to add more filters later -->
<OnMatch>NEUTRAL</OnMatch> 
<OnMismatch>DENY</OnMismatch>
</filter>

<!-- This evaluator triggers e-mail sending. As we only filter 
    events with appropriate
marker, e-mail would always have only one logEvent -->
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>MY_MARKER</marker>
</evaluator>
</appender>

----

No comments:

Post a Comment