2011-04-13 108 views
0

我有一個需求,我需要解析輸入文件並只讀取文件的某些部分。我有一個日誌文件,它有不同的級別,如信息警告和錯誤。現在我只需要讀取包含完整錯誤堆棧跟蹤的部分。我怎樣才能實現這一點使用Java。Java - 如何只讀取輸入流的某些部分

例:

INFO | 2011-04-13 17:59:22,810 | Calling Feedback from 127.0.0.1 
INFO | 2011-04-13 17:59:24,920 | Successfully called Feedback from 127.0.0.1 
INFO | 2011-04-13 17:59:31,561 | FeedBackList 

ERROR | 2011-04-13 19:00:41,640 | 
java.util.concurrent.TimeoutException 
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:228) 
    at java.util.concurrent.FutureTask.get(FutureTask.java:91) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) 
    at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:111) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
    at $Proxy309.getConsumerProfileData(Unknown Source) 
    at com.scea.usps.model.service.impl.AccountSettingsServiceImpl.getUserProfile(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) 
    at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:111) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 
    at $Proxy284.getUserProfile(Unknown Source) 
    at com.scea.usps.model.common.PsninfoUtility.getTop3Generes(Unknown Source) 
    at com.scea.usps.model.common.PsninfoUtility.updatePsnInfoDetail(Unknown Source) 
    at com.scea.platform.framework.api.PsnInfoThread.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:619) 

INFO | 2011-04-13 17:59:22,810 | Calling Feedback from 127.0.0.1 
INFO | 2011-04-13 17:59:24,920 | Successfully called Feedback from 127.0.0.1 
INFO | 2011-04-13 17:59:31,561 | FeedBackList 

在上述日誌I需要提取(讀出)從ERROR開始直到堆棧跟蹤結束的所有行。請在此分享您的想法。謝謝。

+0

當你說「只讀文件的某些部分」你的意思是你需要分析那些文件?我問的是你只是在尋找一種算法來解析適當的數據,或者更復雜的東西? 此外,將所有「錯誤」有堆棧跟蹤?是行ERROR/INFO和換行符的唯一開始條目? 如果這只是一個基本的「我該怎麼......」的問題,那麼Brian的答案是最有意義的。 – Mike 2011-04-13 18:31:36

+0

爲什麼日誌文件解析器必須是Java? – MarkOfHall 2011-04-13 18:33:04

+0

不知道鏈鋸http://logging.apache.org/chainsaw/index.html是否可以處理這種開箱即用,但在構建自己的應用程序之前,我可以嘗試一下。 – Kennet 2011-04-13 18:38:35

回答

0
  • 打開文件
  • 看,直到你擊中錯誤
  • 讀取和處理線的線條,直到你遇到堆棧跟蹤後的空白行。

沖洗,重複。

+0

@mike是的,我只是在尋找一種算法來解析一個巨大的日誌文件中所需的部分。是的,所有的錯誤都會有堆棧跟蹤。 – Hitatichi 2011-04-13 18:52:29

0
try { 
     BufferedReader in 
      = new BufferedReader(new FileReader("logfile.log")); 
     String line = in.readLine(); 
     while (!line.startsWith("ERROR")) { 
     line = in.readLine(); 
     if(line==null){ 
      //throw exception here, ERROR not found in entire log file 
     } 
     } 
     //HERE line will be your error line 
     while (line!=null) { 
     line = in.readLine(); 
     //do something with line 
     } 
     //here you have reached the end of the file 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
+0

感謝您的預測回覆。但是ERROR行之後的行發生了什麼。即使這些行需要與Error行一起打印。請指導我正確的方向。謝謝。 – Hitatichi 2011-04-13 18:42:52

1

如果你的目標是看你的應用程序的錯誤日誌,然後像Chainsaw的工具可能是一個更好的解決方案。

2
BufferedReader in = new BufferedReader(new FileReader("logfile.log")); 
    String line = in.readLine(); 
    StringBuffer buf = null; 
    while (line != null) { 
    if(line.startsWith("ERROR")){ 
     buf = new StringBuffer(); 
     buf.append(line).append("\n"); 
     while(line != null && !line.trim().equals("")){ 
      line = in.readLine(); 
      buf.append(line).append("\n"); 
     } 
     //Now buf has your error an do whatever you want to do with it 
     //then delete 
     buf = null; 
    } 
    line = in.readLine(); 
    } 
+0

感謝您的答覆。它不起作用。我需要打印錯誤堆棧跟蹤的第一行,但我還需要打印錯誤堆棧跟蹤的其餘行。 – Hitatichi 2011-04-13 19:20:17

+0

@Hitatichi:我的解決方案在StringBuffer上保存了「ERROR ...」和空行之間的所有行,所以這也保存了你想要的棧跟蹤 – Dave 2011-04-13 20:25:04