user
user

Reputation: 6947

How to trace Java servlet execution?

I've hit one of those bugs that only seem to show up in production. Whenever I try to reproduce the error on my development machine, even with the same input data, I draw a blank.

So, I want to be able to trace the entire execution flow of this Java servlet which is running through Tomcat 7, from its invocation to the final exception that gets thrown. Ideally, it'd give me a lengthy list of method calls, parameters and return values. Something like the *nix strace, except for Java classes.

I did find Execution trace/flow in java 6 and higher where in the comments BTrace is linked to. I've had a look at it, but don't really see how it might help me. Also, it looks to be centered around running through a separate Java program. I'm OK with adding a bit of code, but really don't want to have to go into every method to add a trace call.

The absolute best would be if whatever approach used to do this can be coaxed into logging through slf4j (since I already use that for logging), but any logging is better than none.

How to obtain such a trace of a servlet?

Upvotes: 0

Views: 1302

Answers (2)

Martin Seeler
Martin Seeler

Reputation: 6982

Maybe the Mapped Diagnostic Context can help you in this situation. Since you're using slf4j, it should be no problem to switch to logback as logging framework.

With it, you can provide additional informations, such as URI, session etc. + you can extend your logging pattern to include method and class to drill down your stack.

Upvotes: 1

Tim B
Tim B

Reputation: 41188

I am not aware of an automatic tool for this but it is fairly easy to do by hand.

Just pick a few relevant places (for example inside the servlet) when it is actually called and do something like:

logger.info("Called X with Params "+y, new Exception());

That will give you the method, the parameters and the stack trace.

Scatter a few more log lines like that in relevant places and you should be able to track down the problem.

Upvotes: 1

Related Questions