拦截器示例之日志记录
2015-12-27 03:59
441 查看
xxx-servlet.xml配置
AllInterceptor.java
测试运行
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="exam.njxzc.Interceptor.AllInterceptor" /> </mvc:interceptor> </mvc:interceptors>
AllInterceptor.java
import java.text.SimpleDateFormat; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.NamedThreadLocal; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class AllInterceptor implements HandlerInterceptor{ protected Logger logger = LoggerFactory.getLogger(getClass()); //定义线程本地变量,这里的NamedThreadLocal只是1个带name属性的ThreadLocal: private static final ThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("ThreadLocal StartTime"); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (logger.isDebugEnabled()){ long beginTime = System.currentTimeMillis();//1、开始时间 startTimeThreadLocal.set(beginTime); //线程绑定变量(该数据只有当前请求的线程可见) logger.debug("开始计时: {} URI: {}", new SimpleDateFormat("hh:mm:ss.SSS") .format(beginTime), request.getRequestURI()); } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if (modelAndView != null){ logger.info("ViewName: " + modelAndView.getViewName()); } } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse resonse, Object handle, Exception ex) throws Exception { // 打印JVM信息。 if (logger.isDebugEnabled()){ long beginTime = startTimeThreadLocal.get();//得到线程绑定的局部变量(开始时间) long endTime = System.currentTimeMillis(); //2、结束时间 logger.debug("计时结束:{} 耗时:{} URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m", new Object[]{ new SimpleDateFormat("hh:mm:ss.SSS").format(endTime), formatDateTime(endTime - beginTime), request.getRequestURI(), Runtime.getRuntime().maxMemory()/1024/1024, Runtime.getRuntime().totalMemory()/1024/1024, Runtime.getRuntime().freeMemory()/1024/1024, (Runtime.getRuntime().maxMemory()-Runtime.getRuntime().totalMemory()+Runtime.getRuntime().freeMemory())/1024/1024}); } } /** * 转换为时间(天,时:分:秒.毫秒) * @param timeMillis * @return */ private static String formatDateTime(long timeMillis){ long day = timeMillis/(24*60*60*1000); long hour = (timeMillis/(60*60*1000)-day*24); long min = ((timeMillis/(60*1000))-day*24*60-hour*60); long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60); long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000); return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss; } }
测试运行
03:47:42,507 DEBUG [exam.njxzc.Interceptor.AllInterceptor] -开始计时: 03:47:42.506 URI: /exammanage/listrooms.do 03:47:42,939 INFO [exam.njxzc.Interceptor.AllInterceptor] -ViewName: jsonView 03:47:42,959 DEBUG [exam.njxzc.Interceptor.AllInterceptor] -计时结束:03:47:42.958 耗时:0:0:0.452 URI: /exammanage/listrooms.do 最大内存: 247m 已分配内存: 168m 已分配内存中的剩余空间: 79m 最大可用内存: 159m
相关文章推荐
- Maven with Scala
- scala + intellij idea 环境搭建及编译、打包
- nginx 安装手记
- HDU 2082 找单词 --- 母函数
- 如何正确看待手机的续航
- 陈坤:一个贫穷而美貌的男人在这世上可能遭遇什么
- Scrum Meeting 14-20151227
- centos7图形配置 firewall-config
- centos7图形配置 firewall-config
- CLR via C#深解笔记三 - 基元类型、引用类型和值类型 | 类型和成员基础 | 常量和字段
- 网页全栈工程师要点分析
- java使用jUnit,jMockit,dbunit做DAO层单元测试方法
- 拦截器与过滤器的区别
- Junit测试编程
- 在Activity之间使用Intent传值和Bundle传值的区别和方式
- 连京东都开始卖翻新机,教你识别手中的iPhone 隐藏id
- notepad++实现多处编辑
- cas client入门:web.xml 配置
- 自定义NSOperation
- ubuntu添加中文输入法