Java - 如何仅读取输入流的某些部分

发布于 2024-11-01 06:47:41 字数 3171 浏览 5 评论 0原文

我有一个要求,我需要解析输入文件并仅读取文件的某些部分。我有一个日志文件,它具有不同的级别,例如信息警告和错误。现在我只需要读取包含完整错误堆栈跟踪的部分。我怎样才能使用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 开始直到堆栈跟踪结束的所有行。请分享您对此的想法。谢谢。

I have a requirement where i need to parse an input file and read only certain parts of the file. I have a an log file which has different levels like info warning and error. Now i need to read only the parts which contains the full error stack trace. How can i achieve this using java.

Ex:

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

In the above log i need to extract(read) all lines starting from ERROR till the stack trace ends. Please share your ideas on this. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧城烟雨 2024-11-08 06:47:41
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();
  }
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();
  }
溺ぐ爱和你が 2024-11-08 06:47:41

如果您的目标是查看应用程序日志中的错误,那么像 Chainsaw 这样的工具可能是更好的解决方案。

If your goal is to watch your application's logs for errors, then a tool like Chainsaw may be a better solution.

在你怀里撒娇 2024-11-08 06:47:41

如果您使用 LogFilePatternReceiver,Chainsaw 确实具有此内置功能。您可以定义一个filterExpression,并且只有与该filterExpression匹配的事件才会被处理。

仅包含堆栈跟踪的示例过滤器表达式为:

存在异常

您必须提供日志文件的格式。有关详细信息,请参阅 JavaDoc:

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

Chainsaw does have this built-in, if you use a LogFilePatternReceiver. You can define a filterExpression, and only events matching that filterexpression will be processed.

An example filter expression that will only include stack traces would be:

exception exists

You do have to provide the format of your log file. See the JavaDoc for more info:

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

与之呼应 2024-11-08 06:47:41
  • 打开文件
  • 读取,直到遇到错误
  • 读取行,并处理行,直到遇到堆栈跟踪后的空行。

冲洗,重复。

  • Open File
  • Read until you hit the line with ERROR
  • Read and process lines until you hit the blank line after the stack trace.

Rinse, Repeat.

固执像三岁 2024-11-08 06:47:41
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();
    }
  }
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();
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文