Email Alerts With Logback

Logback provides a support to send logged messages in an email through an SMTPAppender. The SMTPAppender accumulates logging events in one or more fixed-size buffers and sends the contents of the appropriate buffer in an email after a user-specified event occurs. By default, the email transmission is triggered by a logging event of level ERROR or higher. Moreover, by default, a single buffer is used for all events.

A sample smtp appender configuration would look like below

<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
    <smtpHost>ADDRESS-OF-YOUR-SMTP-HOST</smtpHost>
    <to>EMAIL-DESTINATION</to>

    <from>SENDER-EMAIL</from>
    <subject>TESTING: %logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.PatternLayout">
      <pattern>%date %-5level %logger{35} - %message%n</pattern>
    </layout>
</appender>

The smtpHost tag needs to be specified as “SMTPHost” if you are using logback version older then 0.9.27. This was one of the bugs fixed in 0.9.27.

Make sure the java mail api jar and java beans activation framework jar files are in your classpath. Both of them can be downloaded from here and here respectively.

Buffer Size

As mentioned above the log events are accumulated in an buffer whose default size is 256. It is a cyclic buffer which can hold 256 log messages and it would discard older events (or messages) when the buffer becomes full. The size of the buffer can be configured by the bufferSize property. This helps in keeping the memory requirements bounded.

Below is a sample smtp appender configuration with a custom buffer size specified.

<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
    <smtpHost>${smtpHost}</smtpHost>
    <to>${to}</to>
    <from>${from}</from>
    <subject>%logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.html.HTMLLayout"/>

    <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTrackerImpl">
      <!-- hold just last 5 log entries -->
      <bufferSize>5</bufferSize>
    </cyclicBufferTracker>
</appender>

<root>
    <level value="INFO" />
    <appender-ref ref="EMAIL"/>
</root>

The above configuration to set a custom buffer size works only in logback jar version 0.9.26 and above as noted here.

Accumulating the messages in a buffer is a useful feature provided by logback. If you application is configured with an smtp appender and a buffer size of 5, you would receive an email alert when a error message is logged. The email will also include previous 4 info messages (if logged) as the below sample email message body shows

2011-10-27 19:26:32,871 [main] INFO  test.smtp.SampleApp[27] - Info Message Number : 1
2011-10-27 19:26:32,871 [main] INFO  test.smtp.SampleApp[27] - Info Message Number : 2
2011-10-27 19:26:32,871 [main] INFO  test.smtp.SampleApp[27] - Info Message Number : 3
2011-10-27 19:26:32,871 [main] INFO  test.smtp.SampleApp[27] - Info Message Number : 4
2011-10-27 19:26:32,871 [main] ERROR test.smtp.SampleApp[30] - Error Message

The info messages could give an idea of the application flow and help in troubleshooting the issue faster.

Triggering Event

By default the email transmission is triggered when an error message is encountered (OnErrorEvaluator). It is possible to override this default behavior by providing an implementation of the EventEvaluator interface. More details can be found in this well detailed logback manual.

Invalid email address

If by mistake an invalid email address is configured, a mail is still sent to any valid configured email addresses. Make sure you use at least 0.9.30 version of the library.

Advertisements

Posted on November 2, 2011, in logback and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: