Parmar Kamlesh
Parmar Kamlesh

Reputation: 159

pattern for reading particular stack trace from log file in java

I am reading a log file using BufferedReader and I want to extract a particular stack trace from the log file. However, I cannot read it, the output is empty.

for example I have an exception for the word: client and the stack trace for it is like:

java.sql.SQLException: Unknown column 'client' in 'field list'

SELECT query

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666) at com.mysql.jdbc.Connection.execSQL(Connection.java:2994) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030) at telemed.admin.DAO.AdminDAO.getTeleVisitorData(AdminDAO.java:87) at telemed.admin.AdminService.getTeleVisitors(AdminService.java:31) at org.apache.jsp.jsp.telemed.telemedResponse_jsp._jspService(telemedResponse_jsp.java:330) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

I am trying to read the stack trace which has a particular word in that but not in Exception Line.

my code is:

BufferedReader br=new BufferedReader(new FileReader("E:/exp.txt"));
String temp;
Pattern expPattern=Pattern.compile("(?s).*Exception:.*client.*[\\n\\r].*");

while((temp=br.readLine())!=null){
    //System.out.println("line::::"+temp);
    Matcher headlineMatcher=expPattern.matcher(temp);
    if(headlineMatcher.find()){
        System.out.println("=>"+temp);
        temp=null;
    }
}

Upvotes: 0

Views: 789

Answers (1)

SubOptimal
SubOptimal

Reputation: 22973

The problem with your approach is that readLine() returns a line without any lineend characters. So you cannot match them.

One possible solution is to match the first line and print a fixed number of following lines or until you find a line which can be identified as the last line of your interest.

Take the snippet as simple demonstration.

Pattern expPattern = Pattern.compile("(?s).*Exception:.*client.*");
int followingLines = 4;

try (BufferedReader br = Files.newBufferedReader(Paths.get("exp.txt"), UTF_8)) {
    for (String line = br.readLine(); line != null; line = br.readLine()) {
        if (expPattern.matcher(line).find()) {
            System.out.println("=>" + line);
            for (int i = 0; i < followingLines; i++) {
                line = br.readLine();
                if (line == null) {
                    break;
                }
                System.out.println("=>" + line);
            }
        }
    }
} catch (IOException ex) {
    ex.printStackTrace(System.err);
}

The output is.

=>java.sql.SQLException: Unknown column 'client' in 'field list'
=>
=>SELECT query
=>
=>at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928) at ...

Upvotes: 1

Related Questions