A scenario

Recording Operation Logs

Two code implementation

1 cut class

/** @author ruoyi */ @aspect @Component public class LogAspect {private static final Logger log = LoggerFactory.getLogger(LogAspect.class); / / configure weave points the @pointcut (" @ the annotation (com.sunisco.external.portal.com mon. The annotation. The Log) ") public void logPointCut () {} / * * * * * @afterRETURNING (pointcut = "logPointCut()") public void doAfterReturning(joinPoint joinPoint) { handleLog(joinPoint, null); } @afterthrowing (value = "logPointCut()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Exception e) { handleLog(joinPoint, e); } protected void handleLog(final JoinPoint joinPoint, Final Exception e) {try {// Get the annotation Log controllerLog = getAnnotationLog(joinPoint); if (controllerLog == null) { return; } SysUser currentUser =new SysUser(); / / * = = = = = = = = database log = = = = = = = = = * / / SysOperLog operLog = new SysOperLog (); operLog.setStatus(BusinessStatus.SUCCESS.ordinal()); // Requested address String IP = ""; operLog.setOperIp(ip); operLog.setOperUrl(ServletUtils.getRequest().getRequestURI()); if (currentUser ! = null) { operLog.setOperName(currentUser.getLoginName()); if (StringUtils.isNotNull(currentUser.getDept()) && StringUtils.isNotEmpty(currentUser.getDept().getDeptName())) { operLog.setDeptName(currentUser.getDept().getDeptName()); } } if (e ! = null) { operLog.setStatus(BusinessStatus.FAIL.ordinal()); operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000)); } // Setting method Name String className = joinPoint.gettarGet ().getClass().getName(); String methodName = joinPoint.getSignature().getName(); operLog.setMethod(className + "." + methodName + "()"); / / processing parameters on the annotation set getControllerMethodDescription (controllerLog operLog); // Save database asyncManager.me ().execute(asyncFactory.recordoper (operLog)); } catch (Exception exp) {// Record the local Exception log log.error("== before warning Exception =="); Log.error (" error message :{}", exp. GetMessage (),exp); }} /** * Obtain the method description in the annotation for the Controller layer annotation ** @param log * @param operLog Operation log * @throws Exception */ public void getControllerMethodDescription(Log log, SysOperLog operLog) throws Exception {// Set the action operlog.setBusinesstype (log.businesstype ().ordinal()); // setTitle operlog.settitle (log.title()); // setOperatorType operlog.setoperatortype (log.operatortype ().ordinal())); Request (log.issaverequestData ()) {request (log.issaverequestData ()) {request (log.issaverequestdata); setRequestValue(operLog); }} /** * get the request parameters, * * @param operLog Operation log * @throws Exception */ private void setRequestValue(SysOperLog operLog) throws Exception { Map<String, String[]> map = ServletUtils.getRequest().getParameterMap(); String params = JSON.marshal(map); operLog.setOperParam(StringUtils.substring(params, 0, 2000)); } /** * If there are annotations, If yes, obtain */ private Log getAnnotationLog(JoinPoint JoinPoint) throws Exception {Signature Signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method ! = null) { return method.getAnnotation(Log.class); } return null; }}Copy the code

2 Configure custom annotations for monitoring

/** * @author ruoyi */ @target ({ElementType. METHOD}) @Retention(RetentionPolicy.runtime) @documented public @interface Log {/** * module */ public String title() default ""; /** * function */ public BusinessType BusinessType () default BusinessType.OTHER; /** * Public OperatorType OperatorType () Default OperatorType.MANAGE; Equestdata () public Boolean isSaveRequestData() default true; }Copy the code

Add custom annotations for the method to add logging

/** * delete menu */ @log (title = "menu management ", businessType = BusinessType.DELETE) @GetMapping("/remove/{menuId}") @ResponseBody public AjaxResult remove(@PathVariable("menuId") String menuId) { if (menuService.selectCountMenuByParentId(menuId) > 0) { return Ajaxresult.warn (" submenu exists, deletion not allowed "); } the if (menuService selectCountRoleMenuByMenuId (menuId) > 0) {return AjaxResult. Warn (" menu has been assigned, is not allowed to delete "); } return toAjax(menuService.deleteMenuById(menuId)); }Copy the code

Turn on AOP

AopContext can access @enableAspectJAutoProxy (exposeProxy = true) public by exposing the proxy object through an AOP framework class ApplicationConfig { }Copy the code

Explanation of three parameters

1 @Aspect Specifies the Aspect class

2 @pointcut Common Pointcut expressions, which can be a method in a class, are now annotated for more flexibility

3 JoinPoint: Pass in the section method as the parameter of the function, and you can get the relevant information of the target method

4 @enableAspectJAutoProxy: Enable the annotation based AOP pattern

5 Notification Method

Pre-notification (@before) Post-notification (@after) Return notification (@afterreturning) Exception notification (@Afterthrowing) Surround notification (@around)Copy the code