zoukankan      html  css  js  c++  java
  • aop日志记录

    1.自定义

    package cc.mrbird.common.annotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Log {
        String value() default "";
    }

    2.实现类

    package cc.mrbird.common.aspect;
    
    import cc.mrbird.common.config.FebsProperties;
    import cc.mrbird.common.util.HttpContextUtils;
    import cc.mrbird.common.util.IPUtils;
    import cc.mrbird.system.domain.SysLog;
    import cc.mrbird.system.domain.User;
    import cc.mrbird.system.service.LogService;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import org.apache.shiro.SecurityUtils;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * AOP 记录用户操作日志
     *
     * @author MrBird
     * @link https://mrbird.cc/Spring-Boot-AOP%20log.html
     */
    @Aspect
    @Component
    public class LogAspect {
    
        private Logger log = LoggerFactory.getLogger(this.getClass());
    
        @Autowired
        private FebsProperties febsProperties;
    
        @Autowired
        private LogService logService;
    
    
        @Pointcut("@annotation(cc.mrbird.common.annotation.Log)")
        public void pointcut() {
            // do nothing
        }
    
        @Around("pointcut()")
        public Object around(ProceedingJoinPoint point) throws JsonProcessingException {
            Object result = null;
            long beginTime = System.currentTimeMillis();
            try {
                // 执行方法
                result = point.proceed();
            } catch (Throwable e) {
                log.error(e.getMessage());
            }
            // 获取request
            HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
            // 设置IP地址
            String ip = IPUtils.getIpAddr(request);
            // 执行时长(毫秒)
            long time = System.currentTimeMillis() - beginTime;
            if (febsProperties.isOpenAopLog()) {
                // 保存日志
                User user = (User) SecurityUtils.getSubject().getPrincipal();
                SysLog log = new SysLog();
                log.setUsername(user.getUsername());
                log.setIp(ip);
                log.setTime(time);
                logService.saveLog(point, log);
            }
            return result;
        }
    }

    3.引入

        @Log("获取部门信息")
        @RequestMapping("dept")
        @RequiresPermissions("dept:list")
        public String index() {
            return "system/dept/dept";
        }
  • 相关阅读:
    堆排序优化与几个排序算法时间复杂度
    冒泡排序的实现和优化及其与插入,选择排序的比较
    插入排序的实现与优化并和选择排序进行性能比较
    选择排序的实现以及性能测试
    MD5加密
    低功耗蓝牙BLE [学习笔记]
    反编译[学习笔记]
    Android 混淆[学习笔记]
    北大SQL数据库视频课程笔记
    repo/git Android/CyanogenMod srouce code
  • 原文地址:https://www.cnblogs.com/dand/p/10186538.html
Copyright © 2011-2022 走看看