Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 466
Mike M. Lin Avatar asked Aug 25 '11 16:08

Mike M. Lin


1 Answers

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.

like image 191
nicholas.hauschild Avatar answered Sep 28 '22 13:09

nicholas.hauschild