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
在上面的日志中,我需要提取(读取)从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的目标是查看应用程序日志中的错误,那么像 Chainsaw 这样的工具可能是更好的解决方案。
If your goal is to watch your application's logs for errors, then a tool like Chainsaw may be a better solution.
如果您使用 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
冲洗,重复。
Rinse, Repeat.