簡體   English   中英

Java-如何僅讀取輸入流的某些部分

[英]Java - How to read only certain parts of an input stream

我有一個需要解析輸入文件並僅讀取文件某些部分的要求。 我有一個日志文件,它具有不同級別的信息警告和錯誤。 現在,我只需要閱讀包含完整錯誤堆棧跟蹤的部分。 我如何使用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

在上面的日志中,我需要提取(讀取)從ERROR開始的所有行,直到堆棧跟蹤結束。 請就此分享您的想法。 謝謝。

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();
  }

如果您的目標是查看應用程序的日志中是否有錯誤,那么像Chainsaw這樣的工具可能是一個更好的解決方案。

如果使用LogFilePatternReceiver,則電鋸確實具有此內置功能。 您可以定義filterExpression,只有與之匹配的事件將被處理。

僅包含堆棧跟蹤的示例過濾器表達式為:

存在異常

您必須提供日志文件的格式。 有關更多信息,請參見JavaDoc:

http://logging.apache.org/log4j/companions/receivers/apidocs/org/apache/log4j/varia/LogFilePatternReceiver.html

  • 打開文件
  • 讀取直到出現錯誤為止
  • 讀取並處理行,直到您在堆棧跟蹤之后打到空白行。

沖洗,重復。

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();
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM