I have a CRUD repository in springboot java app. I am trying to measure the latency of the query form APP perspective and log it in the logger. What is a good elegant way to achieve this please ? I am also going to use these logs for monitoring purposes later.
Currently the way I am doing it is -
long start = System.currentTimeMillis();
result = respoistoryDao.findId();
long executionTime = System.currentTimeMillis()-start;
How can I make this better ?
The most elegant and reusable design would be an Aspect. (Spring AOP docs)
First, create an annotation
Then create the Aspect
Once you have these two pieces, just add
@Timedto any method signature, and it will log out the duration for you.