0

i am fairly new in java and currently working in Struts 1.3. My question is more related with java.

Following is the scenario and i am not sure regarding the best approach to be followed.

I want to make a error reporting functionality in my project where in I'm copying stacktrace and inserting it in the database's table say "errorlog" table with type of exception,date, user etc and other fields. But now what i have to do is : I have to identify the package name, file, method and line number in which the exception occurred with the help of server logs that i have when exception occurs. But the problem is that the log gives you the exception, file and the line number but it also contains errors that are not in my packages as in HttpServlet.service(HttpServlet.java:710) etc given below:

java.lang.NullPointerException 
com.candidate.query.CandSearchAction.execute(CandSearchAction.java:34) 
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484) 
org.apache.struts.action.RequestProcessor.proc ess(RequestProcessor.java:274) 
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482) 
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:710) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 

In above case, i got a null pointer exception in my CandSearchAction.java line number 34. Now i want to know how to differentiate or extract only the line that points to my java/jsp file, so that i can filter them accordingly.

Can anyone please help regarding this situation or how to approach this problem.. is there any library that i can use or anything that could help me. Waiting for a response that would help me.

1 Answers1

0

If your code follows the same basic structure you used in your example, you could parse the stack trace for anything starting with 1 of your package names and only use those lines.

Eric Hydrick
  • 3,467
  • 2
  • 28
  • 41
  • how can i identify or list all the packages of my project so that I can compare it with the stacktrace... Is there any specific method or API that i need to use? – user1629770 Feb 20 '13 at 06:51
  • Exception.getStackTrace() returns an array of StackTraceElements. You could grab the first one (the name of the exception), and then from element 1 on you could see if it starts with (). If yes, log it, if not, leave the loop and move on. I don't know of any library or API off-hand that grabs all of your package names, unfortunately. My instinct would be to make a constants class that returns a hard-coded list of those names, so at least there's only 1 place where you have to maintain it. – Eric Hydrick Feb 20 '13 at 17:18
  • yes that what i am doing right now..but i thought there might be a better approach available for getting the same result. Thanks for your reply Eric!! – user1629770 Feb 22 '13 at 05:45
  • Not that I know of, sorry! – Eric Hydrick Feb 22 '13 at 13:57