I'm creating a Log Filter application that will present a report of all the ERROR entries that were found in the application logs, I was wondering what would be the best method to present a few lines of the stack trace along with each error.
The final result would be something like this:
+ ErrorA
- ErrorB
com.package.Class.method(Class.java:666)
com.package.AnotherClass.ADifferentMethodMethod(Class.java:2012)
com.thatOtherPackage.ThatClass.someOtherMethod(ThatClass.java:34)
+ ErrorC
Here's what I have so far:
public JSONArray processFiles(File[] files){
FileReader fr = null;
BufferedReader br = null;
JSONObject jFiles = new JSONObject();
JSONArray jaf = new JSONArray();
try {
for (File file : files) {
jFiles.put("fileName", file.getName());
boolean fileIsOk = true;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
//Thanks to Windows, there's no way to check file.canRead()
//http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6203387
fileIsOk = false;
}
if(fileIsOk) {
br = new BufferedReader(fr);
String line = null;
JSONObject jLogEntries = new JSONObject();
JSONArray jalog = new JSONArray();
int lineNum = 0;
while ((line = br.readLine()) != null) {
if (line.contains("| ERROR |")) {
jLogEntries.put("line " + lineNum, line);
++lineNum;
}
**// TODO: Implement something to print the next 5 lines of the stack trace.**
}
lineNum = 0;
jalog.add(jLogEntries);
jFiles.put("logEntries", jalog);
jaf.add(jFiles);
}
}// end of files iteration
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return jaf;
}
LineNumberReader is your friend.
LineNumberReadr lr = new LineNumberread(br);
while ((line = lr.readLine()) != null) {
if (line.contains("| ERROR |")) {
jLogEntries.put("line " + lr.getLineNumber(), line);
for (int i = 0; i < 5; i++) {
if ((line = lr.readLine()) != null) {
jLogEntries.put("line " + lr.getLineNumber(), line);
}
}
}
You need to break to the outer loop if there are less then 5 lines in stack, I'll leave it to you to figure that out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.