Reading and Parsing Java Exceptions

I have a small system, and I want to offer a product that can analyze errors / exceptions and suggest a possible solution.

So, I need a way to parse java Exception (since I only have them in the logs [I don't want to affect the real system]).

After parsing it, I want to save it to the database and compare it with previously saved exceptions (in some format) so that I can find the closest comparable exception.

I thought of the following idea: "XException in at B at C at D" will be saved as [XException, A, B, C, D], and I will somehow search in my DB: [XException,?,?, ?] Which is the closest. For example: [XException, A, G, C, D] not bad.

What do you think of these ideas?

Any efficient way to analyze exceptions?

Efficient or better ways to determine the distance between two exceptions?

Know any open source that can do this - I'm sure I haven't found it.

Thanks.

+6
source share
2 answers

This is pretty hard work, but here's a demonstration of parsing some real-world exceptions thrown on the fly.

  • the method is called generate_ $ to try to cover the oddly named methods. I am sure I did not cover all cases.
  • restore them back to the java.lang.StackTraceElement list as it seems to be the right type for the job.

the code:

private static List<String> generate_$() { List<String> returnValue = new LinkedList<String>(); Exception[] exceptions = { new ClassCastException(), new NullPointerException(), new IOException("foo") }; for (Exception exception : exceptions) { try { throw exception; } catch (Exception e) { StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); returnValue.add(writer.getBuffer().toString()); } } return returnValue; } public static void main(String[] args) { List<String> examples = generate_$(); for (String trace : examples) { Pattern headLinePattern = Pattern.compile("([\\w\\.]+)(:.*)?"); Matcher headLineMatcher = headLinePattern.matcher(trace); if (headLineMatcher.find()) { System.out.println("Headline: " + headLineMatcher.group(1)); if (headLineMatcher.group(2) != null) { System.out.println("Optional message " + headLineMatcher.group(2)); } } // "at package.class.method(source.java:123)" Pattern tracePattern = Pattern .compile("\\s*at\\s+([\\w\\.$_]+)\\.([\\w$_]+)(\\(.*java)?:(\\d+)\\)(\\n|\\r\\n)"); Matcher traceMatcher = tracePattern.matcher(trace); List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); while (traceMatcher.find()) { String className = traceMatcher.group(1); String methodName = traceMatcher.group(2); String sourceFile = traceMatcher.group(3); int lineNum = Integer.parseInt(traceMatcher.group(4)); stackTrace.add(new StackTraceElement(className, methodName, sourceFile, lineNum)); } System.out.println("Stack: " + stackTrace); } } 

Conclusion:

 Headline: java.lang.ClassCastException Stack: [com.adamish.ExceptionParse.generate_$((ExceptionParse.java:16), com.adamish.ExceptionParse.main((ExceptionParse.java:31)] Headline: java.lang.NullPointerException Stack: [com.adamish.ExceptionParse.generate_$((ExceptionParse.java:17), com.adamish.ExceptionParse.main((ExceptionParse.java:31)] Headline: java.io.IOException Optional message : foo Stack: [com.adamish.ExceptionParse.generate_$((ExceptionParse.java:17), com.adamish.ExceptionParse.main((ExceptionParse.java:31)] 
+4
source

I think this question will be closed because it is too open. SO is for questions that can be given clear and definitive answers.

However, before that happens, I would like to say that this seems like a pretty good idea, and I hope you can make it work. It would be better to focus on those parts of the stack trace that clearly identify immutable information, such as the names of packages, classes, and methods. Regarding the detection of partial or complete matches, I suggest you familiarize yourself with the well-known indexing and matching algorithms. Some well-known algorithms for text searches can be applied, but with β€œatomic” units, method names or class class names if they are letters or words.

Good luck

EDIT: Just thinking about something else. You can focus on making your implementation as versatile as possible for stack traces of many different programming languages, frameworks, etc. This would make the software more reliable and widely applicable.

+2
source

Source: https://habr.com/ru/post/912402/


All Articles