Filtering Legitimate Exception Traces

Up until now all the code is in standard functions. This is mostly because the code was dealing with selecting the files and doing some initial validation. Neither of those tasks have anything to do with the actual exception-handling code. Now, for the exception parsing and analyzing tasks, I am going to define a class with appropriate methods. This way I can distribute and use it as a completely independent module. For example, let's say I wanted to implement a web-based application where my users could submit their exception logs and get some statistics, and I want to be able to reuse this code. Functions that open files and deal with file patterns become obsolete, because there are simply no files to deal with—all data comes from a web server. Similarly, you may also want to analyze data stored in a database, in which case you would have to write an interface to retrieve this data. However, you still can reuse the code that deals with exception stack trace text. So always try to keep your code logically separated.

As I have mentioned, my exception detection mechanism (Listing 7-7) is somewhat naive—I check for the words exception and java in the stack trace body.

Listing 7-7. Validating exceptions def is_exception(self, strace): if strace.lower().find( strace.lower().find( return True else:

return False

This is easy to change, and should you need anything more sophisticated than this simple test, you can rewrite this function to use a more appropriate algorithm for your situation.

Listing 7-8 shows how this detection mechanism fits together with other parts of the class.

Listing 7-8. The basic structure of the exception container class class ExceptionContainer:

<initialise the object>

def insert(self, suspect_body, f_name=""):

lines = suspect_body.strip().split("\n", 1) log_l = lines[0] if self.is_exception(lines[1]):

<update exceptions statistics and couners>

For every suspect log line detected, the insert method will be called. That method will then call the validation function, which checks whether the text supplied is actually a stack trace and should be counted.

Was this article helpful?

0 0

Post a comment