ssm 項目記錄用戶操作日志和異常日志


借助網上參考的內容,寫出自己記錄操作日志的心得!!

我用的是ssm項目使用aop記錄日志;這里用到了aop的切點 和 自定義注解方式;

1、建好數據表:

  數據庫記錄的字段有: 日志id 、操作人、操作人IP、操作時間、操作方法、操作哪個控制層或者服務層、操作說明(記錄用戶操作的詳情說明)、類型、異常信息

 

2、在spring重配置如下:

  因為在spring里我配置了記錄服務層的切點; 所以我在spring-mvc 里面 重新配置了一個可以掃描控制層的切點,使用的是cglib 代理:

  <!-- 啟動AOP AspectJ注解自動代理 -->
  <aop:aspectj-autoproxy />
  <!-- 通知spring使用cglib而不是jdk的來生成代理方法 AOP可以攔截到Controller -->
  <aop:config proxy-target-class="true"></aop:config>

 

3、接下來寫自定義的注解 

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface ArchivesLog { /** * 操作說明 */ public String operteContent() default ""; }

 

4、拿到切點的內容,並且在此存庫

package com.zhkj.jtpdms.log;

import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.zhkj.jtpdms.entity.system.LogInfo; import com.zhkj.jtpdms.entity.system.User; import com.zhkj.jtpdms.service.system.LogInfoService; import com.zhkj.jtpdms.util.GlobalConstant; /** * 操作日志類 * * @author MEIM * */ @Aspect @Component public class ArchivesLogAspect { @Autowired private LogInfoService loginfoService; private static final Logger logger = LoggerFactory.getLogger(ArchivesLog.class); @Pointcut("@annotation(ArchivesLog)") public void controllerAspect() { //System.out.println("切入點..."); } /** * 方法調用后觸發 , 記錄正常操作 * * @param joinPoint * @throws ClassNotFoundException */ @AfterReturning("controllerAspect()") public void after(JoinPoint joinPoint) throws ClassNotFoundException { // 用戶id int userId = getUSerMsg().getId(); // 用戶IP String ip = getUSerMsg().getLoginIp(); // 控制器名 String targetName = getMethodDesc(joinPoint).getController(); // 方法名 String methodName = getMethodDesc(joinPoint).getMethod(); // 操作說明 String operteContent = getMethodDesc(joinPoint).getOperateContent(); LogInfo logInfo = new LogInfo(); logInfo.setUserId(userId); logInfo.setIp(ip); logInfo.setOperateContent(operteContent); logInfo.setMethod(methodName); logInfo.setController(targetName); loginfoService.insertLog(logInfo); } /** * 發生異常,走此方法 * @param joinPoint * @param e */ @AfterThrowing(pointcut = "controllerAspect()", throwing = "e") public void AfterThrowing(JoinPoint joinPoint, Throwable e) { try { // 用戶id int userId = getUSerMsg().getId(); // 用戶IP String ip = getUSerMsg().getLoginIp(); // 控制器名 String targetName = getMethodDesc(joinPoint).getController(); // 方法名 String methodName = getMethodDesc(joinPoint).getMethod(); // 操作說明 String operteContent = getMethodDesc(joinPoint).getOperateContent(); LogInfo logInfo = new LogInfo(); String exMsg = e.getCause().toString(); if (exMsg != null) { int type = 2; logInfo.setUserId(userId); logInfo.setIp(ip); logInfo.setOperateContent(operteContent); logInfo.setMethod(methodName); logInfo.setController(targetName); logInfo.setType(type); logInfo.setExMsg(exMsg); loginfoService.insertLog(logInfo); } } catch (Exception e1) { logger.error(e1.getMessage()); } } /** * 獲取 注解中對方法的描述 * * @return * @throws ClassNotFoundException */ public static LogInfo getMethodDesc(JoinPoint joinPoint) throws ClassNotFoundException { String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] arguments = joinPoint.getArgs(); Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); String operteContent = ""; for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) {
            // 操作說明 operteContent = method.getAnnotation(ArchivesLog.class).operteContent(); break; } } } LogInfo logInfo = new LogInfo(); logInfo.setController(targetName); logInfo.setMethod(methodName); logInfo.setOperateContent(operteContent); return logInfo; } /** * 得到用戶信息 * * @return */ public static User getUSerMsg() { HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); // 獲取session HttpSession session = req.getSession(); User user = (User) session.getAttribute(GlobalConstant.SESSION_USER); return user; } }

 

5、在控制層/服務層加上自定義注解即可成功

   @RequestMapping("/saveUser")
    @ResponseBody
  // 此處加上自定義注解 ,operteContent 是操作說明 @ArchivesLog(operteContent = "新增用戶") public JsonResult saveUser(User user, String check) {
    //... 增加操作 
   }

 

6、查看效果如下:

 

7、注意事項:

     需要在spring-mvc  配置aop !!!

    通知的幾個屬性(@around , @before ,@after ,@afterRetruning,@afterThrowing)需要弄清楚!!

   還有不明白之處歡迎溝通!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM