3

I have the following regex which I obtained Regular expression to parse a log file and find 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). 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. I'm looking to capture that format of exception with a regular expression. Any Exception, not just an 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. I'm having some trouble capturing the entire trace though.

2
  • 1
    It would appear you don't understand regular expressions. There's a number of resources on the web (probably tens of thousands) that you could use to better your understanding. Or, start with the oracle tutorial here: docs.oracle.com/javase/tutorial/essential/regex Commented Jul 3, 2013 at 1:46
  • Here's another good tutorial. Commented Jul 3, 2013 at 1:46

2 Answers 2

4

This is great for your method of using 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.

((?:[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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

This is only consistent for the method you choose to report your logs as. Good primer for you to learn regular expressions, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.