Wednesday, January 20, 2010

Marker based email notifications

Part of the SLF4J API, Markers allow developers to add meta-data to log statements. Not all errors are equal, with some errors being more unusual than others. If you wish to color log statement related to "unusual" errors you could write:
  Marker unusualMarker = MarkerFactory.getMarker("UNUSUAL");
Logger logger = LoggerFactory.getLogger("myPackage.myClass");
logger.error(unusualMarker, "An unusual error occurred.");

whereas a "regular" error might be logged as
  Logger logger = LoggerFactory.getLogger("myPackage.myClass");
logger.error("An error occurred.");

So what do markers give you? Well, for one, you can perform marker-based filtering. For example, turn off logging except for errors marked as UNUSUAL. At QOS.ch, we use markers to trigger outgoing emails for certain types of unusual errors.

Here is the configuration snippet:
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%date%-5level%thread%mdc{user-id}%logger%msg</pattern>
<throwableRenderer class="ch.qos.logback.classic.html.DefaultThrowableRenderer" />
</layout>
<From>...</From>
<SMTPHost>...</SMTPHost>
<Subject>%msg</Subject>
<To>email address of @ the admin</To>
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
<marker>UNUSUAL</marker>
<marker>ANOTHER_UNUSUAL_EVENT</marker>
</evaluator>
</appender>

This way whenever an "unusual" error occurs, the admin is notified via email. The definition of unusual is highly dependent on the application, but the SLF4J and logback combo offers a flexible way of defining and processing "unusualness".

1 Comments:

Blogger JJ said...

Cool! I've set it up, now just need a release for the
ch.qos.logback.classic.boolex.OnMarkerEvaluator class!

12:01 AM  

Post a Comment

<< Home