Logging elapsed time of execution in SpringBoot rest API

不想你离开。 提交于 2020-07-23 09:02:06

问题


It could be a simple solution but I am unable to get it done. I need to log the overall execution time for my request in SpringBoot Rest API. Request always enters to MainController always and can exit from two places-

  1. Main Restcontroller same method or
  2. ExceptionHandlerController handler method

I have created one custom annotation and injecting it to both main Controller and ExceptionController methods and getting elapsed time for individual methods. So here is the problem. I need to add these individual times to calculate the total time which I don't want.

Is there any other way to log this information easily.

Aspect class:

@Aspect
@Component
public class PointsAspect {

    private final static Logger logger = LoggerFactory.getLogger(PointsAspect.class);

    @Around("@annotation(annotation)")
    public Object logMethod(final ProceedingJoinPoint proceedingJoinPoint, final LogAround annotation)
            throws Throwable {
        final long start = System.currentTimeMillis();
        Object obj;
        try {
            logger.debug("Starting...! Method Name - " +proceedingJoinPoint.getSignature().getName());
            obj = proceedingJoinPoint.proceed();
        } finally {
            logger.debug("Exiting...! Method Name - " +proceedingJoinPoint.getSignature().getName() +"Execution Time in Milliseconds:> "+ String.valueOf(System.currentTimeMillis()-start));
        }
        return obj;
    }
}

Marker Interface:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAround {

}

And this is how I am injecting it:

**ExceptionHandlerController.java**

@LogAround
@ExceptionHandler(HttpMessageNotReadableException.class)
public GenericFailureResponse missingRequestBodyException(HttpServletResponse response,
            HttpServletRequest request, Exception ex) throws IOException {
        GenericFailureResponse failureResponse =  new GenericFailureResponse();
        //TODO: exception logic
        return failureResponse;
}



**MainController.java**

@LogAround
 public String getTotalPoints(@RequestParam(required=true) long memberNo,
            HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        //TODO : some logic
        return "something";
 }

回答1:


You can use a simple filter.

@Component
public class LogTimeFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long startTime = System.currentTimeMillis();
        chain.doFilter(request, response);
        long duration = System.currentTimeMillis() - startTime;
        System.out.println("Request take " + duration + " ms");
    }
}


来源:https://stackoverflow.com/questions/56813002/logging-elapsed-time-of-execution-in-springboot-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!