我正在使用没有日志记录的旧Java应用程序,只是将所有信息打印到控制台.通过执行printStackTrace()调用也可以"处理"大多数异常.
简而言之,我只是将System.out和System.error流重定向到日志文件,现在我需要解析该日志文件.到目前为止一切都很好,但我在尝试解析堆栈跟踪的日志文件时遇到问题.
一些代码也被遮挡了,所以我需要通过实用程序应用程序运行堆栈跟踪来去除它们.我正在尝试自动完成所有这些.
我到目前为止最接近的是使用以下方法获取最初的Exception行:
.+Exception[^\n]+
Run Code Online (Sandbox Code Playgroud)
并使用以下方法找到"at ..(..)"行:
(\t+\Qat \E.+\s+)+
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何将它们组合在一起以获得完整的堆栈跟踪.
基本上,日志文件看起来如下所示.没有固定的结构,堆栈跟踪之前和之后的行是完全随机的:
Modem ERROR (AT
Owner: CoreTalk
) - TIMEOUT
IN []
Try Open: COM3
javax.comm.PortInUseException: Port currently owned by CoreTalk
at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:337)
...
at UniPort.modemService.run(modemService.java:103)
Handling file: C:\Program Files\BackBone Technologies\CoreTalk 2006\InputXML\notify
java.io.FileNotFoundException: C:\Program Files\BackBone Technologies\CoreTalk 2006\InputXML\notify (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
...
at com.gobackbone.Store.a.a.handle(Unknown Source)
at com.jniwrapper.win32.io.FileSystemWatcher.fireFileSystemEvent(FileSystemWatcher.java:223)
...
at java.lang.Thread.run(Unknown Source)
Load Additional Ports
... Lots of random stuff
IN []
[Fatal Error] .xml:6:114: The entity name must immediately follow the '&' in the entity reference.
org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
...
at com.gobackbone.Store.a.a.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
看起来你只需要将它们粘贴在一起(并使用换行符作为粘合剂):
.+Exception[^\n]+\n(\t+\Qat \E.+\s+)+
Run Code Online (Sandbox Code Playgroud)
但我会改变你的正则表达式:
^.+Exception[^\n]++(\s+at .++)+
Run Code Online (Sandbox Code Playgroud)
这结合了at...
线之间的空白并使用占有量词来避免回溯.