基于springMVC拦截器实现操作日志统计
生活随笔
收集整理的這篇文章主要介紹了
基于springMVC拦截器实现操作日志统计
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
1.spring配置文件配置。?
<!-- 攔截器 --><mvc:interceptors><!-- 日志攔截器 --><bean class="cn.jeeweb.modules.common.interceptor.LogInterceptor" ><property name="openAccessLog" value="${openAccessLog}" /></bean><mvc:interceptor><mvc:mapping path="/**" /><!-- 需排除攔截的地址 --><mvc:exclude-mapping path="/static/**" /><!-- 需排除攔截的地址 --><mvc:exclude-mapping path="/upload/**" /><bean class="cn.jeeweb.core.interceptor.EncodingInterceptor" /></mvc:interceptor></mvc:interceptors>2.控制類
package cn.jeeweb.modules.common.interceptor;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 cn.jeeweb.core.utils.DateUtils; import cn.jeeweb.modules.sys.utils.LogUtils;/*** * All rights Reserved, Designed By www.zhisuaninfo.com* * @title: LogInterceptor.java* @package cn.jeeweb.modules.common.interceptor* @description: 訪問日志攔截器* @author: gulf* @date: 2018年1月11日 下午12:17:54* @version V1.0* @copyright: 2017 www.zhisuaninfo.com Inc. All rights reserved.**/ public class LogInterceptor implements HandlerInterceptor {private Boolean openAccessLog = Boolean.FALSE;public void setOpenAccessLog(Boolean openAccessLog) {this.openAccessLog = openAccessLog;}private static final ThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("ThreadLocal StartTime");/*** 日志對象*/protected Logger logger = LoggerFactory.getLogger(getClass());@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {if (logger.isDebugEnabled()) {long beginTime = System.currentTimeMillis();// 1、開始時間startTimeThreadLocal.set(beginTime); // 線程綁定變量(該數(shù)據(jù)只有當前請求的線程可見)logger.debug("開始計時: {} URI: {}", new SimpleDateFormat("hh:mm:ss.SSS").format(beginTime),request.getRequestURI());}return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {if (modelAndView != null) {logger.info("ViewName: " + modelAndView.getViewName());}}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {if (openAccessLog) {// 保存日志LogUtils.saveLog(request, handler, ex, null);// 打印JVM信息。if (logger.isDebugEnabled()) {long beginTime = startTimeThreadLocal.get();// 得到線程綁定的局部變量(開始時間)long endTime = System.currentTimeMillis(); // 2、結束時間 // logger.debug("計時結束:{} 耗時:{} URI: {} 最大內存: {}m 已分配內存: {}m 已分配內存中的剩余空間: {}m 最大可用內存: {}m", // new SimpleDateFormat("hh:mm:ss.SSS").format(endTime), // DateUtils.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);}}}}3.工具類
package cn.jeeweb.modules.sys.utils;import java.lang.reflect.Method; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.web.method.HandlerMethod;import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.google.common.collect.Lists; import com.google.common.collect.Maps;import cn.jeeweb.core.utils.CacheUtils; import cn.jeeweb.core.utils.Exceptions; import cn.jeeweb.core.utils.IpUtils; import cn.jeeweb.core.utils.SpringContextHolder; import cn.jeeweb.core.utils.StringUtils; import cn.jeeweb.modules.sys.entity.Log; import cn.jeeweb.modules.sys.entity.Menu; import cn.jeeweb.modules.sys.entity.User; import cn.jeeweb.modules.sys.service.ILogService; import cn.jeeweb.modules.sys.service.IMenuService; import cn.jeeweb.modules.sys.tags.SysFunctions;public class LogUtils {public static final String CACHE_MENU_NAME_PATH_MAP = "menuNamePathMap";private static ILogService logService = SpringContextHolder.getBean(ILogService.class);private static IMenuService menuService = SpringContextHolder.getBean(IMenuService.class);/*** 保存日志*/public static void saveLog(HttpServletRequest request, String title) {saveLog(request, null, null, title, null);}/*** 保存日志*/public static void saveLog(HttpServletRequest request, String title, String content) {saveLog(request, null, null, title, content);}public static void saveLog(HttpServletRequest request, Object handler, Exception ex, String title) {saveLog(request, handler, ex, title, null);}/*** 保存日志*/public static void saveLog(HttpServletRequest request, Object handler, Exception ex, String title, String content) {User user = UserUtils.getUser();if (user != null && user.getId() != null) {Log log = new Log();log.setTitle(title);log.setType(ex == null ? Log.TYPE_ACCESS : Log.TYPE_EXCEPTION);log.setRemoteAddr(IpUtils.getIpAddr(request));log.setUserAgent(request.getHeader("user-agent"));log.setRequestUri(request.getRequestURI());log.setParams(request.getParameterMap());log.setMethod(request.getMethod());log.setContent(content);// 異步保存日志new SaveLogThread(log, handler, ex).start();}}/*** 保存日志線程*/public static class SaveLogThread extends Thread {private Log log;private Object handler;private Exception ex;public SaveLogThread(Log log, Object handler, Exception ex) {super(SaveLogThread.class.getSimpleName());this.log = log;this.handler = handler;this.ex = ex;}@Overridepublic void run() {// 獲取日志標題if (StringUtils.isBlank(log.getTitle())) {String permission = "";if (handler instanceof HandlerMethod) {Method m = ((HandlerMethod) handler).getMethod();RequiresPermissions rp = m.getAnnotation(RequiresPermissions.class);permission = (rp != null ? StringUtils.join(rp.value(), ",") : "");}log.setTitle(getMenuNamePath(log.getRequestUri(), permission));}// 如果有異常,設置異常信息log.setException(Exceptions.getStackTraceAsString(ex));// 如果無標題并無異常日志,則不保存信息if (StringUtils.isEmpty(log.getTitle()) && StringUtils.isEmpty(log.getException())) {return;}// 保存日志信息logService.insert(log);}}/*** 獲取菜單名稱路徑(如:系統(tǒng)設置-機構用戶-用戶管理-編輯)*/public static String getMenuNamePath(String requestUri, String permission) {String url = StringUtils.substringAfter(requestUri, SysFunctions.getAdminUrlPrefix() + "/");@SuppressWarnings("unchecked")Map<String, String> menuMap = (Map<String, String>) CacheUtils.get(CACHE_MENU_NAME_PATH_MAP);if (menuMap == null) {menuMap = Maps.newHashMap();List<Menu> menuList = menuService.selectList(new EntityWrapper<Menu>());for (Menu menu : menuList) {// 獲取菜單名稱路徑(如:系統(tǒng)設置-機構用戶-用戶管理-編輯)String namePath = "";if (menu.getParentIds() != null) {List<String> namePathList = Lists.newArrayList();for (String id : StringUtils.split(menu.getParentIds(), "/")) {/** if (Menu.getRootId().equals(id)){ continue; // 過濾跟節(jié)點* }*/for (Menu m : menuList) {if (m.getId().equals(id)) {namePathList.add(m.getName());break;}}}namePathList.add(menu.getName());namePath = StringUtils.join(namePathList, "-");}// 設置菜單名稱路徑if (StringUtils.isNotBlank(menu.getUrl())) {menuMap.put(menu.getUrl(), namePath);} else if (StringUtils.isNotBlank(menu.getPermission())) {for (String p : StringUtils.split(menu.getPermission())) {menuMap.put(p, namePath);}}}CacheUtils.put(CACHE_MENU_NAME_PATH_MAP, menuMap);}String menuNamePath = menuMap.get(url);if (menuNamePath == null) {for (String p : StringUtils.split(permission)) {menuNamePath = menuMap.get(p);if (StringUtils.isNotBlank(menuNamePath)) {break;}}if (menuNamePath == null) {return "";}}return menuNamePath;} }?
?
我的博客即將搬運同步至騰訊云+社區(qū),邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan
轉載于:https://my.oschina.net/chendongj/blog/1605606
總結
以上是生活随笔為你收集整理的基于springMVC拦截器实现操作日志统计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java】SAX解析character
- 下一篇: 机器学习资料合计(二)