接口处理时间打印

public class RequestTimeInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(RequestTimeInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();
        request.setAttribute("startTime", startTime);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long startTime = (Long)request.getAttribute("startTime");
        request.removeAttribute("startTime");
        long executeTime = System.currentTimeMillis() - startTime;
        log.info("接口:{},处理类:{},处理耗时:{}ms",request.getRequestURI(), handler, executeTime);
    }
}

 

posted @ 2022-01-11 09:52  皮军旗  阅读(92)  评论(0编辑  收藏  举报