开发者_StackOverflowwe are gathering lot of logs, we are going to use a database or a file to store these logs and want to be able to search through them using text search. Is there a way to do this efficiently.
So what you got to do is
ExceptionPersistingService eps = new ExceptionPersistingService();
try{
//some code that might trow exception
}catch(Exception e){
eps.saveNewExceptin(getStackTrace(e));
}
public static String getStackTrace(Throwable aThrowable) {
//add the class name and any message passed to constructor
final StringBuilder result = new StringBuilder("Trace: ");
result.append(aThrowable.toString());
final String NEW_LINE = "<br>";
result.append(NEW_LINE);
//add each element of the stack trace
for (StackTraceElement element : aThrowable.getStackTrace()) {
result.append(element);
result.append(NEW_LINE);
}
return result.toString();
}
Than you create lucene implementation (I recommend using hibernate search) that will index the the exception strings that you stored in the database. In your save() method you can create object PersistentException that has ID
, Date
, ExceptionString
, User
, and even possibly URL
details at which the exception occurred.
Than all you have to do is parse your queries, create Fuzzy or any other query and enjoy the search results. For more details you will have to study books like Hibernate Search in Action
and Lucene in Action
. They have pretty good examples on how to do what I just briefly mentioned.
精彩评论