正则表达式解析日志文件并查找堆栈跟踪 [英] Regular expression to parse a log file and find stacktraces

查看:208
本文介绍了正则表达式解析日志文件并查找堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用没有日志记录的旧Java应用程序,只是将所有信息打印到控制台。通过执行printStackTrace()调用也可以处理大多数异常。

I'm working with a legacy Java app that has no logging and just prints all information to the console. Most exceptions are also "handled" by just doing a printStackTrace() call.

简而言之,我刚刚将System.out和System.error流重定向到日志文件,现在我需要解析该日志文件。到目前为止一切都很好,但我在尝试解析堆栈跟踪的日志文件时遇到了问题。

In a nutshell, I've just redirected the System.out and System.error streams to a log file, and now I need to parse that log file. So far all good, but I'm having problems trying to parse the log file for stack traces.

一些代码也被遮挡了,所以我需要运行堆栈通过实用程序应用程序来去除它们。我正在尝试自动完成所有这些。

Some of the code is obscufated as well, so I need to run the stacktraces through a utility app to de-obscufate them. I'm trying to automate all of this.

我到目前为止最接近的是使用这个来获取最初的Exception行:

The closest I've come so far is to get the initial Exception line using this:

.+Exception[^\n]+

使用以下方式查找at ..(..)行:

And finding the "at ..(..)" lines using:

(\t+\Qat \E.+\s+)+

但我无法弄清楚如何将它们放在一起以获得完整的堆栈跟踪。

But I can't figure out how to put them together to get the full stacktrace.

基本上,日志文件看起来如下所示。没有固定的结构,堆栈跟踪之前和之后的行是完全随机的:

Basically, the log files looks something like the following. There is no fixed structure and the lines before and after stack traces are completely random:

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)


推荐答案

看起来你只需将它们粘贴在一起(和使用换行作为胶水):

Looks like you just need to paste them together (and use a newline as glue):

.+Exception[^\n]+\n(\t+\Qat \E.+\s+)+

但我会改变你的正则表达式:

But I would change your regex a bit:

^.+Exception[^\n]++(\s+at .++)+

这结合了 at ... 行之间的空格和使用占有量词来避免回溯。

This combines the whitespace between the at... lines and uses possessive quantifiers to avoid backtracking.

这篇关于正则表达式解析日志文件并查找堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆