// Path: src/main/java/com/zlwh/hands/api/common/utils/LogUtils.java
// public class LogUtils {
// 	
// 	private static CLogDao logDao = SpringContextHolder.getBean(CLogDao.class);
// 	private static Map<String,String[]> uriLogTypeMapping = new HashMap<String,String[]>();
// 	static{
// 		uriLogTypeMapping.put("/hands/api/user/1.0/register", new String[]{"0","注册"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/login", new String[]{"1","登录"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/logout", new String[]{"2","退出"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/resetPassword", new String[]{"3","重置密码"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/modifyPhone", new String[]{"4","更换手机号"});
// 	}
// 	
// 	/**
// 	 * 保存日志
// 	 */
// 	public static void saveLog(HttpServletRequest request, Object handler, Exception ex){
// 			CLog log = new CLog();
// 			String[] logType = uriLogTypeMapping.get(request.getRequestURI());
// 			if (logType != null) {
// 				log.setType(logType[0]);
// 				log.setTitle(logType[1]);
// 			}
// 			log.setUserToken(request.getParameter("userToken"));
// 			log.setRemoteAddr(StringUtils.getRemoteAddr(request));
// 			log.setUserAgent(request.getHeader("user-agent"));
// 			log.setRequestUri(request.getRequestURI());
// 			log.setParams(request.getParameterMap());
// 			log.setMethod(request.getMethod());
// 			log.setCreateTime(new Date());
// 			// 异步保存日志
// 			new SaveLogThread(log, handler, ex).start();
// 	}
// 
// 	/**
// 	 * 保存日志线程
// 	 */
// 	public static class SaveLogThread extends Thread{
// 		
// 		private CLog log;
// 		private Exception ex;
// 		
// 		public SaveLogThread(CLog log, Object handler, Exception ex){
// 			super(SaveLogThread.class.getSimpleName());
// 			this.log = log;
// 			this.ex = ex;
// 		}
// 		
// 		@Override
// 		public void run() {
// 			// 如果有异常，设置异常信息
// 			log.setException(Exceptions.getStackTraceAsString(ex));
// 			// 如果无标题并无异常日志，则不保存信息
// 			if (StringUtils.isBlank(log.getTitle()) && StringUtils.isBlank(log.getType())){
// 				return;
// 			}
// 			// 保存日志信息
// 			logDao.insert(log);
// 		}
// 	}
// 
// }


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;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.zlwh.hands.api.common.utils.LogUtils;
/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zlwh.hands.api.interceptor;





/**
 * 日志拦截器
 * @author ThinkGem
 * @version 2014-8-19
 */
public class LogInterceptor implements HandlerInterceptor {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	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 response, 
			Object handler, Exception ex) throws Exception {

		// 保存日志
// Path: src/main/java/com/zlwh/hands/api/common/utils/LogUtils.java
// public class LogUtils {
// 	
// 	private static CLogDao logDao = SpringContextHolder.getBean(CLogDao.class);
// 	private static Map<String,String[]> uriLogTypeMapping = new HashMap<String,String[]>();
// 	static{
// 		uriLogTypeMapping.put("/hands/api/user/1.0/register", new String[]{"0","注册"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/login", new String[]{"1","登录"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/logout", new String[]{"2","退出"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/resetPassword", new String[]{"3","重置密码"});
// 		uriLogTypeMapping.put("/hands/api/user/1.0/modifyPhone", new String[]{"4","更换手机号"});
// 	}
// 	
// 	/**
// 	 * 保存日志
// 	 */
// 	public static void saveLog(HttpServletRequest request, Object handler, Exception ex){
// 			CLog log = new CLog();
// 			String[] logType = uriLogTypeMapping.get(request.getRequestURI());
// 			if (logType != null) {
// 				log.setType(logType[0]);
// 				log.setTitle(logType[1]);
// 			}
// 			log.setUserToken(request.getParameter("userToken"));
// 			log.setRemoteAddr(StringUtils.getRemoteAddr(request));
// 			log.setUserAgent(request.getHeader("user-agent"));
// 			log.setRequestUri(request.getRequestURI());
// 			log.setParams(request.getParameterMap());
// 			log.setMethod(request.getMethod());
// 			log.setCreateTime(new Date());
// 			// 异步保存日志
// 			new SaveLogThread(log, handler, ex).start();
// 	}
// 
// 	/**
// 	 * 保存日志线程
// 	 */
// 	public static class SaveLogThread extends Thread{
// 		
// 		private CLog log;
// 		private Exception ex;
// 		
// 		public SaveLogThread(CLog log, Object handler, Exception ex){
// 			super(SaveLogThread.class.getSimpleName());
// 			this.log = log;
// 			this.ex = ex;
// 		}
// 		
// 		@Override
// 		public void run() {
// 			// 如果有异常，设置异常信息
// 			log.setException(Exceptions.getStackTraceAsString(ex));
// 			// 如果无标题并无异常日志，则不保存信息
// 			if (StringUtils.isBlank(log.getTitle()) && StringUtils.isBlank(log.getType())){
// 				return;
// 			}
// 			// 保存日志信息
// 			logDao.insert(log);
// 		}
// 	}
// 
// }


// Path: src/main/java/com/zlwh/hands/api/interceptor/LogInterceptor.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;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.zlwh.hands.api.common.utils.LogUtils;

/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zlwh.hands.api.interceptor;





/**
 * 日志拦截器
 * @author ThinkGem
 * @version 2014-8-19
 */
public class LogInterceptor implements HandlerInterceptor {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	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 response, 
			Object handler, Exception ex) throws Exception {

		// 保存日志
		LogUtils.saveLog(request, handler, ex);