2 Replies Latest reply on May 13, 2009 11:18 PM by Francisco Jose Peredo Noguez

    send email when fatal exception is thrown

    Arbi Sookazian Master

      we're contemplating sending emails to help desk so help desk has a heads up prior to end user calling in a ticket for a Seam app problem.

      anybody have ideas on how best to implement this and what exceptions specifically to trap and what data to include in the email (stack trace, userid, siteid, etc)?

        • 1. Re: send email when fatal exception is thrown
          Clint Popetz Apprentice

          I have a number of hooks in various places to catch exceptions, each of which raises a single event which is observed by the code that emails, as well code that logs to the database, to log4j, and to jms.  Having a single event means adding a new reporting mechanism is easier.

          The problem is where to raise the event.  I have wicket, jsf, and struts code running in the same app.  So I do this:

          • put create a @Filter(within="org.jboss.seam.web.contextFilter") that catches all exceptions and raises the above event. 

          • I add no global jsf exception handler, so that my filter always catches things. 

          • For wicket, I create a SeamWebRequestCycle subclass that overrides onRuntimeexception to raise the event. 

          • For resource servlets I override AbstractResource.doWork() and raise it there. 

          • For quartz jobs I also install my own:

          public class AsynchExceptionHandler extends AsynchronousExceptionHandler
            @In ServerEnvironment serverEnvironment;
            public void handleException(Exception throwable)

          to raise it and I also add a:


          that raises it if there is an underlying exception in jobWasExecuted

          Those without such contorted legacies to maintain will find it easier, of course.  I don't recommend on relying on pages.xml for exception handling, as there are several cases that won't be caught by that mechanism.

          In terms of what to put in the data you save, my recommendation is everything.  I save request parameters, a snapshot of the session, the state of the contexts, the jsf phase id, the time, the logged in user, the stack trace, etc.  Whatever you can, and have intelligent code on the other end sort out what's what, and classify it, and create jira issues based on it :)

          • 2. Re: send email when fatal exception is thrown
            Francisco Jose Peredo Noguez Master

            I use SFL4J and Logback,both created by the same guy that invented log4j (and of course backwards compatibility is available if needed), both are fast and easy, just drop the appropiate .jars in WEB-INF\lib, add the logback.xml config file. Example:

            <?xml version="1.0" encoding="UTF-8"?>
            <configuration debug="true">
              <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
                <layout class="ch.qos.logback.classic.PatternLayout">
                  <!-- <pattern>%d{ABSOLUTE} %5p %c:%L - %m%n</pattern>-->
                  <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
               <!-- Email configuration-->
                         <appender name="email" class="ch.qos.logback.classic.net.SMTPAppender">
                      <Subject>Error in system: %logger{20} - %m</Subject>
                      <layout class="ch.qos.logback.classic.PatternLayout">
                      <Pattern>%date %-5level %logger{35} - %message%n</Pattern>          
              <!-- Root -->
              <!-- set log levels - for more verbose logging change 'info' to 'debug'
                  ALL< TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
                <level value="WARN"/>
                <appender-ref ref="stdout"/>
                <appender-ref ref="email"/>
               <!-- Seam -->
               <logger name="org.jboss.seam">
                <level value="DEBUG"/>

            And make sure that page.xml treats fatal exceptions as errors:

            <exception log-level="error">
                    <redirect view-id="/error.xhtml">
                        <message>Unexpected fatal error, please try again</message>

            Oh, and remember to configure your web.xml and components.xml to run in production mode, and that is it, easy email notification if anything crashes.