我正在使用BufferedReader
读取日志文件,我想从日志文件中提取特定的堆栈跟踪。但是,我无法读取它,输出为空。
例如,我有一个例外:客户端 并且它的堆栈跟踪如下:
java.sql.SQLException:'字段列表'中的未知列'client'
SELECT查询
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)at at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)at at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)at at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936) 在 com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030) at telemed.admin.DAO.AdminDAO.getTeleVisitorData(AdminDAO.java:87)at at telemed.admin.AdminService.getTeleVisitors(AdminService.java:31)at org.apache.jsp.jsp.telemed.telemedResponse_jsp._jspService(telemedResponse_jsp.java:330) 在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 在 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
我正在尝试读取堆栈跟踪,其中包含特定字,但不在异常行中。
我的代码是:
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;
}
}
答案 0 :(得分:1)
您的方法存在的问题是,readLine()
会返回行,而不会任何行端字符。所以你无法匹敌它们。
一种可能的解决方案是匹配第一行并打印固定数量的后续行,或者直到找到可以识别为您感兴趣的最后一行的行。
将代码段作为简单演示。
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);
}
输出是。
=>java.sql.SQLException: Unknown column 'client' in 'field list'
=>
=>SELECT query
=>
=>at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928) at ...