Reporting exceptions


Until now, you have looked at various exception logging strategies. After the exception is logged, there is also a need to report the fatal and serious ones by sending out emails and pager messages to the support team. Several approaches exist and the choices are numerous , but in this chapter we would like to consolidate the logging and error reporting for better coordination and control. For this, let us look at what Log4J has to offer.

As you already know, Log4J has three main components : Layout , Appender , and Category (also known as Logger ). Layout represents the format of the message to be logged. Appender is an alias for the physical location at which the message will be logged. And category is the named entity; you can think of it as a handle for logging. Layouts and Appenders are declared in an XML configuration file. Every category comes with its own layout and Appender definitions. When you get a category and log to it, the message ends up in all the appenders associated with that category, and all those messages will be represented in the layout format specified in the XML configuration file.

Log4J comes with several standard appenders and one of them is called SMTPAppender . By using the SMTPAppender , you can declaratively send email messages when the errors occur in your system. You can configure the SMTPAppender like any other Appender ‚ in the Log4J configuration file. Listing 9.17 shows a sample setup for SMTPAppender . It takes a Threshold , beyond which the Appender is operational, a subject for the email, the From and To addresses, SMTP server name and the pattern for the email message body.

Listing 9.17: SMTP Appender setup
 <appender name="Mybank-Mail"          class="org.apache.log4j.net.SMTPAppender">    <param name="Threshold" value="ERROR" />    <param name="Subject" value="Mybank Online has problems" />    <param name="From" value="prod-monitor@mybank.com" />    <!-- use commas in value to separate multiple recipients -->     <param name="To" value="prod-support@mybank.com " />        <param name="SMTPHost" value="mail.mybank.com" />    <layout class="org.apache.log4j.PatternLayout">       <param name="ConversionPattern" value="%m" />    </layout> </appender> 
 

You can set up the category for the above SMTPAppender as

 <category name=com.mybank.webtier.action additivity=false>    <priority value=ERROR/>    <appender-ref ref=Mybank-Mail/>    <appender-ref ref=Mybank-ErrorLog/> </category> 

With this setup all the exceptions that are caught in the base Struts Action ‚ MybankBaseAction are reported to email address prod-support@mybank.com. This is because the category name is identified by the package name for MybankBaseAction and while logging in Listing 9.16, we used the category whose name is same as the fully qualified class name for MybankBaseAction which happens to be com.mybank.webtier.action.MybankBaseAction . The email address prod-support@mybank.com is generally an email group configured in the enterprise mail server to include several individual recipients. Alternatively, you can explicitly specify multiple recipients in the To param in Listing 9.17 with commas separating the recipients. You can take a similar approach if you are logging in the Base JSP class of Listing 9.14 or the custom tag class of 9.15. But what if you are logging the exception using a scriptlet in the JSP. Although this approach is not recommended, suppose that you already have it in place and want to retrofit the Log4J email feature. In this case, you still can setup the appender as in Listing 9.17. But what about the jsp? What is the fully qualified class name for the JSP? This depends on the vendor. For instance, in weblogic a JSP in a directory called mortgage will reside in package named jsp_servlet.mortgage . Accordingly, for WebLogic, you can setup the category as follows

 <category name="  jsp_servlet.mortgage  " additivity="false">    <priority value="ERROR"/>    <appender-ref ref="Mybank-Mail"/>    <appender-ref ref="Mybank-ErrorLog"/> </category> 

Note that this setting is vendor specific and may not be portable to other application servers. But this is a minor change and should not be a problem if you decide to port to another application server say JBoss.

If you are wondering, ‚“Email messages are all fine. How do I send pager beeps? ‚½ The quick answer is ‚“No problem ‚½. Pagers have email addresses too. You can ask your service provider to associate the email address with the pager. Telecommunications companies and providers can use JavaMail API to implement a PAGER transport protocol that sends email messages to alphanumeric pagers . Similar approach works for Short Message Service (SMS) too since you can email a SMS device.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net