在 SpringBoot rest 中记录执行时间 API
Logging elapsed time of execution in SpringBoot rest API
这可能是一个简单的解决方案,但我无法完成。
我需要在 SpringBoot Rest API 中记录我的请求的整体执行时间。
请求总是进入到MainController
总是并且可以从两个地方退出-
- 主要
Restcontroller
相同方法或
ExceptionHandlerController
处理方法
我创建了一个自定义注释并将其注入主要 Controller
和 ExceptionController
方法,并获取各个方法的运行时间。
所以这就是问题所在。我需要添加这些单独的时间来计算我不想要的总时间。
有没有其他方法可以轻松记录此信息。
看点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;
}
}
标记接口:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAround {
}
我是这样注射的:
**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";
}
您可以使用简单的过滤器。
@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");
}
}
这可能是一个简单的解决方案,但我无法完成。
我需要在 SpringBoot Rest API 中记录我的请求的整体执行时间。
请求总是进入到MainController
总是并且可以从两个地方退出-
- 主要
Restcontroller
相同方法或 ExceptionHandlerController
处理方法
我创建了一个自定义注释并将其注入主要 Controller
和 ExceptionController
方法,并获取各个方法的运行时间。
所以这就是问题所在。我需要添加这些单独的时间来计算我不想要的总时间。
有没有其他方法可以轻松记录此信息。
看点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;
}
}
标记接口:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAround {
}
我是这样注射的:
**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";
}
您可以使用简单的过滤器。
@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");
}
}