简体   繁体   English

Java正则表达式匹配记录器中的异常堆栈跟踪

[英]Java regular expression to match exception stack trace from logger

I have the following regex which I obtained Regular expression to parse a log file and find stacktraces 我有以下正则表达式,我获得了正则表达式来解析日志文件并查找stacktraces

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

It works great to find stack traces. 查找堆栈跟踪非常有用。 However, I need to find stack traces that are outputted through a logger (in a Bukkit Minecraft server, specifically). 但是,我需要找到通过记录器(特别是在Bukkit Minecraft服务器中)输出的堆栈跟踪。 They look like this: 他们看起来像这样:

2012-08-10 08:19:17 [SEVERE] java.lang.NullPointerException
2012-08-10 08:19:17 [SEVERE] at net.minecraft.server.World.tickEntities(World.java:1146)
2012-08-10 08:19:17 [SEVERE] at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:567)
2012-08-10 08:19:17 [SEVERE] at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
2012-08-10 08:19:17 [SEVERE] at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
2012-08-10 08:19:17 [SEVERE] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
2012-08-10 08:19:17 [SEVERE] at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)

The date and times will always change, but the [SEVERE] will always be there. 日期和时间将始终更改,但是[SEVERE]将始终存在。 I'm looking to capture that format of exception with a regular expression. 我正在寻找使用正则表达式捕获异常的格式。 Any Exception, not just an NPE. 任何例外,而不仅仅是NPE。

I'm really not sure how to go about this. 我真的不知道该怎么做。 I checked out some tutorials on regex but these bigs ones are all gibberish to me still. 我查看了一些有关正则表达式的教程,但是这些大教程对我来说还是很乱的。 This is my most recent attempt 这是我最近的尝试

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

Edit: Okay, I did some more research and got a little further. 编辑:好的,我做了一些更多的研究,并且做得更多。 I made this: \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\[SEVERE\\] Which matches the first part. 我做到了: \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\[SEVERE\\]第一部分。 I'm having some trouble capturing the entire trace though. 我在捕获整个跟踪时遇到了一些麻烦。

This is great for your method of using Logger. 这非常适合您使用Logger的方法。 By default the stack traces print out with tabs for every line of the stack trace. 默认情况下,堆栈跟踪的每一行都带有选项卡打印出来。 My example matches the beginning line with the date and original error message plus the Exception error and stack traces if they are there. 我的示例将起始行与日期和原始错误消息以及Exception错误和堆栈跟踪(如果它们存在)相匹配。

((?:[a-zA-Z]{3} \d{1,2}, \d{4,4} \d{1,2}:\d{2}:\d{2} (AM|PM) (\(INFO\)|\(SEVERE\)|\(WARNING\))).*\r(?:(.*Exception.*(\r.*)(\tat.*\r)+)))|((?:[a-zA-Z]{3} \d{1,2}, \d{4,4} \d{1,2}:\d{2}:\d{2} (AM|PM) (\(INFO\)|\(SEVERE\)|\(WARNING\))).*)

Which matches for any of the following: 符合以下任一条件:

Feb 08, 2014 5:18:00 PM (SEVERE) Thread: 13 [com.datarefresh.refresh.RefreshActionQueuePersister.write] Could not write RefreshActionQueue checkpoint due to error
Feb 09, 2014 1:00:10 AM (INFO) Thread: 14 [com.datarefresh.refresh.RefreshWorker.doPostProcess] (DATA_INSIGHT_DATA_REFRESH.15) Post-processing...
Feb 09, 2014 1:00:20 AM (SEVERE) Thread: 14 [com.datarefresh.refresh.RefreshActionQueuePersister.write] Could not write RefreshActionQueue checkpoint due to error
java.lang.RuntimeException: Could not delete RefreshActionQueue checkpoint file
    at com.datarefresh.refresh.RefreshActionQueuePersister.delete(RefreshActionQueuePersister.java:71)
    at com.datarefresh.refresh.RefreshActionQueuePersister.write(RefreshActionQueuePersister.java:53)
    at com.refresh.RefreshActionQueue.persist(RefreshActionQueue.java:94)
    at com.refresh.RefreshActionQueue.removeCurrentAction(RefreshActionQueue.java:48)
    at com.refresh.RefreshWorker.doPostProcess(RefreshWorker.java:304)
    at com.refresh.RefreshWorker.doActions(RefreshWorker.java:82)
    at com.refresh.RefreshWorker.call(RefreshWorker.java:57)
    at com.datarefresh.refresh.RefreshWorker.call(RefreshWorker.java:28)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Feb 09, 2014 1:00:20 AM (INFO) Thread: 14 [com.datarefresh.refresh.RefreshWorker.doPostProcess] (DATA_INSIGHT_DATA_REFRESH.16) Done post-processing.

In reality I would remove INFO from the matching. 实际上,我将从匹配项中删除INFO。

I did it guys! 我做到了!

^.+Exception[^\n]++(\s\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \[SEVERE\] at .++)+

You just have to turn on DOTALL when you compile it. 编译时只需要打开DOTALL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM