Defining log annotations

/** ** Target indicates where the annotation is used * Retention indicates when the annotation is retained ** Documented indicates javadoc */ @target (ElementType.METHOD) @Retention(RetentionPolicy.runtime) @Documented Public @interface SysLog {// Module name String module() default""; String operation() default""; // Operation type OperateType OperateType () default OperateType.OTHER; /** Whether to save request parameters */ Boolean equestparam () defaulttrue;
}
Copy the code

Define log facets

@slf4j @component public class SysLogAspect {** * define Pointcut */ @pointcut ("@annotation(com.xup.common.annotion.SysLog)")
    public void logPointCut() {} /** * Around ** @param point join point * @throws exception */ @around ("logPointCut()") public void around(ProceedingJoinPoint joinPoint) throws Throwable { long beginTime = System.currentTimeMillis(); // Call joinPoint.proceed(); // Execution duration (ms) long executeTime = System.currentTimemillis () -beginTime; // Save logs handleLog(joinPoint, executeTime, null); } private SysLog getAnnotationLog(JoinPoint JoinPoint) {Signature Signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod();if(method ! = null) {return method.getAnnotation(SysLog.class);
        }
        returnnull; } @param joinPoint * @param e exception */ @afterThrowing (value ="logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) { handleLog(joinPoint,0, e); } protected void handleLog(Final JoinPoint JoinPoint, Long executeTime, Final Exception e) {try {// Get the comment SysLoglogAnnotation = getAnnotationLog(joinPoint);
            if (logAnnotation == null) {
                return; } / / * = = = = = = = = database log = = = = = = = = = * / / com xup. Sys. Config. The entity. The SysLog SysLog = new com. Xup. Sys. Config. The entity. The SysLog (); sysLog.setStatus(BusinessStatus.SUCCESS.ordinal()); sysLog.setExecuteTime(executeTime); String ip = ShiroUtils.getIp(); sysLog.setIp(ip); sysLog.setUrl(ServletUtils.getRequest().getRequestURI()); User currentUser = shiroutils.getSysuser ();if(currentUser ! = null) { sysLog.setOperator(currentUser.getLoginName()); }if(e ! = null) { sysLog.setStatus(BusinessStatus.FAIL.ordinal()); sysLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000)); } // Setting method Name String className = joinPoint.gettarGet ().getClass().getName(); String methodName = joinPoint.getSignature().getName(); sysLog.setMethod(className +"." + methodName + "()"); // Process the information on the Settings annotationsetAnnotationInfo(sysLog, logAnnotation); } catch (Exception exp) {log.error()"Log entry exception :{}", exp.getMessage()); exp.printStackTrace(); } /** * Get the description of the method in the annotation for the Controller layer annotation * @param sysLog * @paramlogAnnotation Operation log */ private voidsetAnnotationInfo(com.xup.sys.config.entity.SysLog sysLog,
           SysLog logAnnotation) {
        sysLog.setModule(logAnnotation.module());
        sysLog.setOperation(logAnnotation.operation());
        sysLog.setOperateType(logAnnotation.operateType().ordinal());
       
        if (logAnnotation.saveRequestParams()) { Map<String, String[]> map = ServletUtils.getRequest().getParameterMap(); String params = JSONObject.toJSONString(map); sysLog.setParam(StringUtils.substring(params, 0, 2000)); }}Copy the code

Add annotations to methods

@SysLog("Save menu")
public void save(@RequestBody SysMenu menu){
    
}
Copy the code