RestLogInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { millisecond = System.currentTimeMillis() - millisecond; String location = request.getHeader("X-FORWARDED-FOR"); if (CommonUtil.isBlank(location)) { String address = request.getRemoteAddr(); String host = request.getRemoteHost(); if (!CommonUtil.isBlank(address)) { location = address; } else if (!CommonUtil.isBlank(host)) { location = host; } } String url = request.getRequestURI(); if (request.getQueryString() != null) { url += "?" + request.getQueryString(); } String info = location + ", " + request.getMethod() + " " + url + ", " + response.getStatus() + ", " + millisecond + "ms"; logger.info(info); String currentTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH).format(System.currentTimeMillis()); CommonUtil.writeFile(auditFile, currentTime + " AUDIT " + info + "\n", true); }  @Override boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler); @Override void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView); @Override void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);  }
@Test(groups = { "TestRestLogInterceptor" }) public void testAfterCompletion() { RestLogInterceptor testLogInterceptor = new RestLogInterceptor(); try { testLogInterceptor.afterCompletion(request, response, handler, ex); } catch (Exception e) { e.printStackTrace(); } }