根据java中的正则表达式阅读

时间:2014-02-07 12:39:01

标签: java regex stack-trace

我的输入是sample.log

13 Aug 2013 11:28:30,414 [WebContainer : 6] ERROR - An Error has occured for  com.marsh.framework.core.exception.MarshException: Your session has timed out.
13 Aug 2013 11:28:30,414 [WebContainer : 6] ERROR - handleException():com.marsh.framework.core.exception.MarshException: Your session has timed out.
 at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.prepareUserContext(CSAAbstractStrutsAction.java(Compiled Code))
at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.preexecute(CSAAbstractStrutsAction.java(Compiled Code))
at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.execute(CSAAbstractStrutsAction.java(Compiled Code))
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java(Inlined Compiled Code))
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java(Compiled Code))
at org.apache.struts.action.ActionServlet.process(ActionServlet.java(Inlined Compiled Code))
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java(Compiled Code))
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java(Compiled Code))
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java(Compiled Code))
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java(Compiled Code))
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java(Compiled Code))
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java(Compiled Code))
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))
  13 Aug 2013 11:28:30,414 [WebContainer : 6] ERROR - An Error has occured for com.marsh.framework.core.exception.MarshException: Your session has timed out.
  13 Aug 2013 11:28:30,414 [WebContainer : 6] ERROR - handleException():com.marsh.framework.core.exception.MarshException: Your session has timed out.
at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.prepareUserContext(CSAAbstractStrutsAction.java(Compiled Code))
at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.preexecute(CSAAbstractStrutsAction.java(Compiled Code))
at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.execute(CSAAbstractStrutsAction.java(Compiled Code))
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java(Inlined Compiled Code))
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java(Compiled Code))
at org.apache.struts.action.ActionServlet.process(ActionServlet.java(Inlined Compiled Code))
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java(Compiled Code))
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java(Compiled Code))
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java(Compiled Code))
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java(Compiled Code))
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java(Compiled Code))
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java(Compiled Code))
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java(Compiled Code))
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))

我只想将每个日志条目输出为一个事件,甚至将整个堆栈跟踪作为一个事件输出。

类似这样的事情

   13 Aug 2013 11:28:30,414 [WebContainer : 6] ERROR - An Error has occured for  com.marsh.framework.core.exception.MarshException: Your session has timed out.

   13 Aug 2013 11:28:30,414 [WebContainer : 6] ERROR -  handleException():com.marsh.framework.core.exception.MarshException: Your session has timed out.
   at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.prepareUserContext(CSAAbstractStrutsAction.java(Compiled Code))
   at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.preexecute(CSAAbstractStrutsAction.java(Compiled Code))
   at com.marsh.csa.serviceagreement.CSAAbstractStrutsAction.execute(CSAAbstractStrutsAction.java(Compiled Code))
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java(Inlined Compiled Code))
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java(Compiled Code))
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java(Inlined Compiled Code))
   at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java(Compiled Code))
   at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
   at javax.servlet.http.HttpServlet.service(HttpServlet.java(Compiled Code))
   at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java(Compiled Code))
  at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java(Compiled Code))
  at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java(Compiled Code))
 at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java(Compiled Code))
 at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java(Compiled Code))
 at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java(Compiled Code))
  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java(Compiled Code))
 at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java(Compiled Code))
 at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java(Compiled Code))
 at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java(Compiled Code))
 at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java(Compiled Code))
 at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))

    Scanner sc = new Scanner(new File("D:/sample.log"));

    sc.findInLine("(\\S*\\d+\\s\\w+\\s\\d+\\s\\d+\\:\\d+\\:\\d+\\,\\d+)\\S*");

  while (sc.hasNext()) {
      System.out.println(sc.nextLine()+"\n");

但是上面的代码分别给出了每一行。使用扫描仪可以将整个堆栈跟踪分组为一个事件吗?

1 个答案:

答案 0 :(得分:0)

我认为Scanner不是这项工作的合适工具。扫描程序用于使用分隔符将文件分解为标记,然后可选地解析这些标记。您似乎只是试图测试一行日志文件是否是日志语句的开头。逐行读取文件并测试每个文件是否启动日志语句会更有意义。

Pattern pattern = Pattern.compile("(\\S*\\d+\\s\\w+\\s\\d+\\s\\d+\\:\\d+\\:\\d+\\,\\d+)\\S*");
BufferedReader reader = new BufferedReader(new FileReader(file));
String inputLine;
String outputLine = null;
while ((inputLine = reader.readLine()) != null) {
    if (outputLine == null) {
        outputLine = inputLine;
    } else if (pattern.matches(inputLine)) {
        System.out.println(outputLine);
        outputLine = inputLine;
    } else {
        outputLine += inputLine;
    }
}
System.out.println(outputLine);
相关问题