I want to print the all the lines along with exception name after the Exception occurs in a log file. The lines starting with "at XYZ.ABC.( abcabcabc java:519)
till Truncated. see log file for complete stacktrace. The problem is it also prints the line containing **** Error Wed Jul 05 22:23:30 GMT 2017 1499293410496 and **** Warning Wed Jul 05 22:23:30 GMT 2017 1499293410496.
My Code
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
public class findException {
public static void main(String[] args)
{
try
{
FileInputStream fis = new FileInputStream("log path");
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
BufferedWriter bw = new BufferedWriter(new FileWriter("Trace_failed.txt"));
String strLine;
Boolean check= false;
int line = 0;
while((strLine = br.readLine()) != null)
{
if(strLine.contains("Servlet failed with Exception") || (strLine.contains("java.lang.Exception")) || (strLine.contains("SQLException")))
{
check = true;
}
if(check)
{
bw.write(strLine);
bw.newLine();
line++;
}
if(!strLine.contains("Truncated. see log file for complete stacktrace"))
{
check = false;
line = 0;
}
}
dis.close();
bw.close();
System.out.println("Written Successfully");
}
catch(Exception e)
{
System.err.println("Error " + e.getMessage());
}
}
}
Please scroll right for the exception written. I have just written 2 of them. So what do I to remove all the lines having ****Error... and ****Warning... .
I stopped the loop when it reads Truncated. see log file for complete stacktrace
Please Help!!
The best solution would be to use standard logging solutions - See Log4J or Logback or some other readily available frameworks. They provide very good options to to print stacktraces into log files. Another solution that could be used with or without logging frameworks is to use an Open source library that I wrote which provides a utility (among few others) that extracts full or filtered out stacktrace from exception into a String. All you have to do is:
Here is a link to an article that explains in details where to get the library or/and its source code (maven or github) and how to use it: Open Source Java library with stack trace filtering, Silent String parsing Unicode converter and Version comparison. Look for paragraph "Stacktrace noise filter"