2014-02-07 29 views
0
以正則表達式

我輸入sample.log讀按照在Java

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"); 

但上面的代碼分別給每個行。掃描儀可以將整個堆棧跟蹤分組爲一個事件嗎?

回答

0

我不認爲掃描儀是這份工作的正確工具。掃描儀用於使用分隔符將文件分解爲令牌,然後可以選擇解析這些令牌。您似乎只是試圖測試日誌文件的一行是否是日誌語句的開始。逐行讀取文件並測試每個文件是否啓動日誌語句會更有意義。

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); 
+0

我對上述答案的一些不滿讓我更仔細地看待這個問題。您的正則表達式的最後一位\\ S *正嘗試匹配非空白字符,但堆棧軌跡中顯然有空白。另外,由於您並沒有像我這樣編譯您的正則表達式模式,所以它不會匹配行邊界。此外,你正在調用'sc.findInLine',它只能一行一行地進行。最後,你需要從你找到的任何堆棧跟蹤中刪除換行符,讓它按照你想要的方式打印。 –

+0

嗨大衛, 我想你的代碼,但它給我錯誤在其他如果(pattern.matches(inputLine))作爲未解決的編譯問題: \t方式匹配(字符串,CharSequence)類型模式不適用爲參數(字符串) – prad

+0

而是嘗試'pattern.matcher(inputLine).matches()' –