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="email@example.com"/> <param name="From" value="firstname.lastname@example.org"/> <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.