2 Replies Latest reply on May 13, 2009 11:18 PM by luxspes

    send email when fatal exception is thrown

    gonorrhea

      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
          cpopetz

          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:



          @Scope(ScopeType.STATELESS)
          @Name("org.jboss.seam.async.asynchronousExceptionHandler")
          public class AsynchExceptionHandler extends AsynchronousExceptionHandler
          {
          
            @In ServerEnvironment serverEnvironment;
            public void handleException(Exception throwable)
            {
          


          to raise it and I also add a:


          QuartzDispatcher.instance().getScheduler().addGlobalJobListener


          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

            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">
                <Target>System.out</Target>
                <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>
                </layout>
              </appender>
              
              
              
               <!-- Email configuration-->
                         <appender name="email" class="ch.qos.logback.classic.net.SMTPAppender">
                      <SMTPHost>XX.XX.XX.XX</SMTPHost>          
                      <SMTPPort>25</SMTPPort>
                      <To>somone@domain.com</To>    
                      <To>somoneelse@otherdomain.com</To>    
                      <Subject>Error in system: %logger{20} - %m</Subject>
                      <Username>emailusername</Username>
                      <Password>emailpassword</Password>
                      <layout class="ch.qos.logback.classic.PatternLayout">
                      <Pattern>%date %-5level %logger{35} - %message%n</Pattern>          
                      </layout>         
                      </appender>
            
                 
              
              <!-- Root -->
              <!-- set log levels - for more verbose logging change 'info' to 'debug'
                  ALL< TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
              <root>
                <level value="WARN"/>
                <appender-ref ref="stdout"/>
                <appender-ref ref="email"/>
              </root>
              
              
              
               <!-- Seam -->
              <!-- 
              
               <logger name="org.jboss.seam">
                <level value="DEBUG"/>
              </logger>-->
              
              
              
            </configuration>
            
            



            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>
                    </redirect>
                </exception>
            



            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.