A slightly better Log4j SMTP Appender …

Log4j already offers a SMTPAppender that can be used to send error messages by email. Unfortunately, this appender doesn’t allow you to compose the mail subject from the log message. This is annoying when you start receiving many messages and you have to go through all of them in order to understand which are duplicates and what the severity of the error is.

I’ve put together a replacement SMTP appender that behaves exactly like log4j’s own, with the exception that it has a new property named ‘subjectSuffix’. The subject suffix can be set to a layout pattern.

For example, a log4j configuration like the one below will populate the subject of the email with the message being logged prefixed with the text “Errors”.

...
<appender name="EMAIL" class="ca.intelliware.commons.log4j.SmtpAppender">
  <param name="Threshold" value="ERROR"/>
  <param name="To" value="noname@intelliware.ca"/>
  <param name="From" value="noname@intelliware.ca"/>
  <param name="Subject" value="Errors"/>
  <param name="SubjectSuffix" value="%m"/>
  <param name="SMTPHost" value="mail.intware.com"/>
</appender>
...

The code has been committed to subversion under http://subversion.intelliware.ca/rep/intelliware/commons/trunk/log4jGoodies.

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply