Handling Runtime Exceptions By Redirecting The Logs Using Log4j

Recently I came across a requirement where all the Runtime Exceptions(Unchecked) that are occured during runtime should be redirected to file with the help of log4j.
Below are the some of the examples of RuntimeExceptions

java.net.SocketException: Connection reset
java.lang.IllegalStateException: Response already committed
java.lang.ClassCastException: flex.messaging.HttpFlexSession
java.rmi.RemoteException
org.apache.beehive.netui.pageflow.internal.UnhandledException: java.net.SocketException: Write failed: Broken pipe
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

One way is to handle all the above exceptions in the application using try/catch or Throws. But if they are not handled in the application(and it cannot be done
always as these are runtime exceptions), and during runtime if it occurs it won't be good to show the exception to the end clients. It would be good if we redirect
the page to underconstruction page and also the exception should be known to System Admin or the Application Support Team.

Now we are going to see how we can acheive this scenario:

Let say you have a JSP as shown below:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Demo</title>  
 </head>  
 <body>  
 <%  
 String name[] = new String[3];  
 out.println(name[4]);  
 %>  
 </body>  
 </html>  

If you execute this you will hit into java.lang.ArrayIndexOutOfBoundsException.

So in web.xml you should create an error page

   <servlet>  
       <servlet-name>ApplicationExceptionHandler</servlet-name>  
       <servlet-class>com.exception.ApplicationExceptionHandler</servlet-class>  
   </servlet>  
   <servlet-mapping>  
       <servlet-name>ApplicationExceptionHandler</servlet-name>  
       <url-pattern>/applicationExceptionHandler</url-pattern>  
   </servlet-mapping>  
   <error-page>  
     <exception-type>java.lang.Exception</exception-type >  
     <location>/applicationExceptionHandler</location>  
   </error-page>  

Now whenever any exception occurs whether it is java.lang.ArrayIndexOutOfBoundsException or java.lang.ClassCastException etc the application calls

ApplicationExceptionHandler servlet because all the Exceptions are inherited from java.lang.Exception.

Here in this servlet we will get the information like from where the exception is triggered.


 Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");  
 String servletName = (String)request.getAttribute("javax.servlet.error.servlet_name");  
 String requestUri = (String)request.getAttribute("javax.servlet.error.request_uri");  

The exceptions are redirected to logs using Logj. Now in the logs you will observe as shown below

 [2012-06-04 11:15:12,343] ERROR | | (com.exception.ApplicationExceptionHandler) | Servlet Name : TestArithmeticException  
 [2012-06-04 11:15:12,343] ERROR | | (com.exception.ApplicationExceptionHandler) | Exception Type :java.lang.ArithmeticException  
 [2012-06-04 11:15:12,343] ERROR | | (com.exception.ApplicationExceptionHandler) | The request URI: /TestLogger/testArithmeticException  
 [2012-06-04 11:15:12,343] ERROR | | (com.exception.ApplicationExceptionHandler) | The exception message: / by zero  
 [2012-06-04 11:15:15,312] ERROR | | (com.exception.ApplicationExceptionHandler) | Servlet Name : /arrayIndexOutofBound.jsp  
 [2012-06-04 11:15:15,312] ERROR | | (com.exception.ApplicationExceptionHandler) | Exception Type :java.lang.ArrayIndexOutOfBoundsException  
 [2012-06-04 11:15:15,312] ERROR | | (com.exception.ApplicationExceptionHandler) | The request URI: /TestLogger/arrayIndexOutofBound.jsp  


Download the Project.

Once you downloaded the project, in the file TestLogger\src\log4j.xml, modify the line  <param name="File" value="D:/bea121/workspace/TestLogger/LoggingTest.log"/> to as per your environment and deploy and execute http://<hostname>:<port>/TestLogger/arrayIndexOutofBound.jsp


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today