Spring StopWatch使用实例详解
这篇文章主要介绍了Spring StopWatch使用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
StopWatch简单的秒表,允许多个任务的计时,暴露每个命名任务的总运行时间和运行时间。隐藏使用System.currentTimeMillis(),提高应用程序代码的可读性并减少计算错误的可能性。
以下演示使用StopWatch记录请求摘要日志信息:
@Slf4j public class PerformanceInteceptor implements HandlerInterceptor { private ThreadLocal<StopWatch> stopWatchThreadLocal = new ThreadLocal<>(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { StopWatch sw = new StopWatch(); stopWatchThreadLocal.set(sw); sw.start(); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { stopWatchThreadLocal.get().stop(); stopWatchThreadLocal.get().start(); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { StopWatch sw = stopWatchThreadLocal.get(); sw.stop(); String method = handler.getClass().getSimpleName(); if (handler instanceof HandlerMethod) { String beanType = ((HandlerMethod) handler).getBeanType().getName(); String methodName = ((HandlerMethod) handler).getMethod().getName(); method = beanType + "." + methodName; } // sw.getTotalTimeMillis(), 总执行时间 //sw.getTotalTimeMillis() - sw.getLastTaskTimeMillis(), 执行方法体所需要的时间 log.info("{};{};{};{};{}ms;{}ms;{}ms", request.getRequestURI(), method, response.getStatus(), ex == null ? "-" : ex.getClass().getSimpleName(), sw.getTotalTimeMillis(), sw.getTotalTimeMillis() - sw.getLastTaskTimeMillis(), sw.getLastTaskTimeMillis()); stopWatchThreadLocal.remove(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http
这篇文章主要介绍了SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http.HttpServletRequest not present解决办法,文中通过代码示例给大家介绍的非常详细,需要的朋友可以参考下2024-03-03Mybatis-Plus中and()和or()的使用与原理详解
最近发现MyBatisPlus还是挺好用的,下面这篇文章主要给大家介绍了关于Mybatis-Plus中and()和or()的使用与原理的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2022-09-09Java中HashSet、LinkedHashSet和TreeSet区别详解
这篇文章主要介绍了Java中HashSet、LinkedHashSet和TreeSet区别详解,如果你需要一个访问快速的Set,你应该使用HashSet,当你需要一个排序的Set,你应该使用TreeSet,当你需要记录下插入时的顺序时,你应该使用LinedHashSet,需要的朋友可以参考下2023-09-09oracle+mybatis-plus+springboot实现分页查询的实例
本文主要介绍了oracle+mybatis-plus+springboot实现分页查询,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-08-08Maven默认中央仓库(settings.xml 配置详解)
这篇文章主要介绍了Maven默认中央仓库(settings.xml 配置详解),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12
最新评论