zoukankan      html  css  js  c++  java
  • 通过aop添加日志管理

    1.使用spring 的 aop 技术切到自定义注解上,所以先创建一个自定义注解类

    import java.lang.annotation.*;
    
    @Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
    @Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
    @Documented //生成文档
    public @interface MyLog {
        String value()  default "";//功能模块名称
        int type() default 1;//用户具体操作类型代表,0:登录;1:查询;2:新增;3:修改;import com.alibaba.fastjson.JSONObject;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestAttributes;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import java.lang.reflect.Method;
    import java.util.Map;
    
    /**
     * 系统日志:切面处理类
     */
    @Aspect
    @Component
    public class SysLogAspect {
        @Resource
        private IViapLogInfoService viapLogInfoService;
    
        //定义切点 @Pointcut
        //在注解的位置切入代码
        @Pointcut("@annotation( com.harzone.lhps.viap.interceptor.MyLog)")
        public void logPoinCut() {
        }
    
        //切面 配置通知
        @AfterReturning("logPoinCut()")
        public void saveSysLog(JoinPoint joinPoint) {
            try {
                ViapLogInfo viapLogInfo = new ViapLogInfo();
            //获取request RequestAttributes ra
    = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; HttpServletRequest request = sra.getRequest();
            //获取客户端ip viapLogInfo.setIp(WebUtil.getHost(request));
    //保存日志 //从切面织入点处通过反射机制获取织入点处的方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //获取切入点所在的方法 Method method = signature.getMethod(); //获取请求的类名 // String className = joinPoint.getTarget().getClass().getName(); //获取请求的方法名 // String methodName = method.getName(); //请求的参数 // Object[] args = joinPoint.getArgs(); //获取request中的json字符串 JSONObject obj=GetRequestJsonUtils.getRequestJsonObject(request); // String params = JSON.toJSONString(obj); //获取操作 MyLog myLog = method.getAnnotation(MyLog.class); if (myLog != null) { if(obj != null) { JSONObject jo = new JSONObject(); if (myLog.status() == 1) { // viapLogInfo.setOperateCondition(className + "." + params); for (Map.Entry<String, Object> entry : obj.entrySet()) { if(entry.getValue() != null && !"".equals(entry.getValue())) { jo.put(entry.getKey(),entry.getValue()); } } } viapLogInfo.setOperateCondition(jo.toString()); } viapLogInfo.setValue(myLog.value()); } viapLogInfoService.add(viapLogInfo); } catch (Exception e) { e.printStackTrace(); } }

    3.接下来就可以在需要监控的方法上添加 aop的自定义注解格式为 @+自定义注解的类名 @MyLog

      @PostMapping("login.json")
        @MyLog(value= "登录"")
        public JSONObject login(@RequestBody User userser) {
            
            return null;
        }
    

      

  • 相关阅读:
    07.15 first与first-child的区别
    7.15 css与js 选择奇偶子元素的区别
    7.15过有意思的生活
    7.14养成健身习惯
    8080端口被占用
    Vue自定义指令和认识钩子函数
    按键修饰符
    Vue 的过滤器
    列表渲染
    在Vue中使用.class样式
  • 原文地址:https://www.cnblogs.com/liaoyanglong/p/10702264.html
Copyright © 2011-2022 走看看