9/04/2009

Log4J: Sending only ERRORs and FATALs to SMTPAppender

SMTPAppender is a very nice way to notify support personnel about a serious error occuring in our application. Unfortunately when configured by the log4j.properties file, there is no easy way to have sent only the ERROR and FATAL messages. By using the XML to configure Log4J, we can achieve this aim by using filters.

LevelRangeFilter can be configured to have sent only ERRORs and FATALs:

<appender class="org.apache.log4j.net.SMTPAppender" name="smtp">
  <param name="To" value="Support@yourcompany.com" />
  <param name="From" value="" />
  <param name="Subject" value="An error occured" />
  <param name="SMTPHost" value="localhost" />
  <param name="BufferSize" value="100" />
  <param name="Threshold" value="ERROR" />
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{HH:mm:ss} %-5p %t %c - %m%n" />
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="ERROR" />
    <param name="LevelMax" value="FATAL" />
  </filter>
</appender>

If you want to include information about the exception in the subject, see here.

No comments:

Post a Comment