Mike M. Lin Mike M. Lin - 11 months ago 64
Java Question

How do you trace the entry into and exit from all functions in Spring-based web app?

On the Spring-based web app project I'm on now, the developers have written two logging statements into every function. One logging the entry into the function, the other logging the exit. The idea is to trace the execution path -- at least a function level.

Is there a way to accomplish this without littering the entire code base with these repetitive statements?

To be clear, we want to log all functions, not just public methods of Spring beans.

Answer Source

You can do this with Spring AOP.

Spring AOP will allow you to 'intercept' method calls and perform arbitrary code before and/or after the method call. You will also be able to examine parameters and return values of these methods.

To achieve the logging you are talking about, you would do something like this, likely using the around advice:

  1. Intercept all method calls (or preferably, only the ones you are interested in).
  2. Do your 'before method' logging.
  3. Invoke the method.
  4. Do your 'after method' logging.

All of the Spring details about how to do this can be found in the documentation provided.