我在记录文件中记录了我的错误。该文件的内容采用以下格式。
INFO: Trying to find a cam application file.
Jul 14, 2014 3:14:33 PM com.tgcs.scrt.ui.components.cam.CamProcessor start
INFO: Processing cam file at c:/scrt/cam/index.cam
Jul 14, 2014 3:14:33 PM com.tgcs.scrt.ui.components.cam.CamProcessor start
WARNING: Error parsing composite application markup (CAM) file: c:/scrt/cam/index.cam . Falling back to log viewer.
Throwable occurred: org.xml.sax.SAXParseException: The end-tag for element type "page" must end with a '>' delimiter.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at com.tgcs.scrt.ui.components.cam.CamProcessor.build(CamProcessor.java:425)
at com.tgcs.scrt.ui.components.cam.CamProcessor.start(CamProcessor.java:557)
at com.tgcs.scrt.client.application.MainPart.createComposite(MainPart.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:600)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
at org.eclipse.e4.core.internal.di.InjectorImpl.processAnnotated(InjectorImpl.java:877)
at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:119)
at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:333)
我只想从文件中捕获以下行
WARNING: Error parsing composite application markup (CAM) file: c:/scrt/cam/index.cam . Falling back to log viewer.
Throwable occurred: org.xml.sax.SAXParseException: The end-tag for element type "page" must end with a '>' delimiter.
目前我正在使用以下实施
private String createErrorMessage( String logContent ) {
String[] messages = logContent.split( "\n" );
StringBuffer message= new StringBuffer();
for(int i=0;i<messages.length;i++) {
String tempMessage = messages[i];
if(tempMessage.indexOf( "Throwable occurred" ) >=0) {
message = message.append( "\n" ).append( messages[i-1]).append( "\n" ).append( messages[i]);
}
}
return message.toString();
}
但我相信会有更好的版本。我只是不想通过拆分内容来创建临时数组。
答案 0 :(得分:0)
如果您不想拆分字符串并创建临时数组,则可以使用正则表达式,如下所示:
Pattern pattern = Pattern.compile("WARNING.*\\nThrowable.*\\n");
Matcher matcher = pattern.matcher(error);
if(matcher.find())
return matcher.group();