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, we use markers to trigger outgoing emails for certain types of unusual errors.

Here is the configuration snippet:
<appender name="EMAIL" class="">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<throwableRenderer class="ch.qos.logback.classic.html.DefaultThrowableRenderer" />
<To>email address of @ the admin</To>
<evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">

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 comment:

JJ said...

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