max
max

Reputation: 1266

jboss 6.x: sending e-Mails if specific type of Exception is thrown

We use jBoss logging and got it finally working - now we have the problem that we dont want to send an e-Mail everytime an ERROR accures. We just want to send an e-Mail if defined Exceptions are thrown. e.g. com.myproject.exceptions.fatal.ProjectFatalException

The default definition in the "jboss-logging.xml" looks like this:

   <log4j-appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
      <error-manager>
         <only-once/>
      </error-manager>

  <level name="ERROR"/>
  <properties>
     <property name="to">[email protected]</property>
     <property name="from">[email protected]</property>
     <property name="subject">JBoss Sever Errors</property>
     <property name="SMTPHost">localhost</property>
     <property name="bufferSize">10</property>
  </properties>

  <formatter>
     <pattern-formatter pattern="%d %-5p [%c] (%t) %m%n"/>
  </formatter>
</log4j-appender> 

Upvotes: 1

Views: 1186

Answers (1)

CoolBeans
CoolBeans

Reputation: 20800

Although this answer is almost 3 months late, hopefully this still helps you and others in the future. You can use the StringMatchFilter to limit the email to a certain error.

You can solve this problem with a combination of SMTPAppender and filters. Add the following in your jboss-log4j.xml:-

<!-- add the SMTPAppender -->
<appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
     </layout> 
     <param name="Threshold" value="ERROR"/>
     <param name="To" value="[email protected]"/>
     <param name="From" value="[email protected]"/>
     <param name="Subject" value="JBoss Alert: ProjectFatalException"/>
     <param name="SMTPHost" value="some.smtp.host"/>
     <param name="BufferSize" value="512"/>
     <filter class="org.apache.log4j.varia.LevelRangeFilter">
         <param name="LevelMin" value="error" />
         <param name="LevelMax" value="fatal" />
     </filter>
     <filter class="org.apache.log4j.varia.StringMatchFilter">
           <param name="StringToMatch" value="ProjectFatalException" />
           <param name="AcceptOnMatch" value="true" />
     </filter>
     <filter class="org.apache.log4j.varia.DenyAllFilter"/>
   </appender> 
    <!-- add the SMTP appender to your project package category -->
    <category name="com.myproject">
         <priority value="ERROR"/>
         <appender-ref ref="SMTP"/>
    </category>

As a side note, generally it is a good idea to add slow appenders like SMTP as ASYNC. By default it is synchronous.

Upvotes: 5

Related Questions