日志管理-aop

来源:互联网 发布:线切割手动圆形编程 编辑:程序博客网 时间:2024/05/22 12:25

日志管理操作一般分为aop和拦截器两种,我做的是aop,但用aop有一个缺点,在做复杂登录验证上是无法获得登录的用户id


自定义注解

 * 自定义日志注解 * @author biling */@Target({ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ServiceLog {    /**     * 日志类型     * @return     */     String logType() default "";    /**     * 日志内容     * @return     */     String logContent() default "";}

切面

import com.gxt.databean.Log;import com.gxt.service.LogService;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import javax.annotation.Resource;import java.lang.reflect.Method;import java.util.Date;@Aspect@Componentpublic class LogAspect {    @Resource    private LogService logService;    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);    @Pointcut("@annotation(com.gxt.annotation.ServiceLog)")    public void serviceAspect() {    }    @Before("serviceAspect()")    public void doBefore(JoinPoint joinPoint) {        if(logger.isInfoEnabled()){            logger.info("before " + joinPoint);        }    }    //配置service环绕通知,使用在方法aspect()上注册的切入点    @Around("serviceAspect()")    public void around(JoinPoint joinPoint){        System.out.println("==========开始执行controller环绕通知===============");        long start = System.currentTimeMillis();        try {            ((ProceedingJoinPoint) joinPoint).proceed();            long end = System.currentTimeMillis();            if(logger.isInfoEnabled()){                logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!");            }            System.out.println("==========结束执行service环绕通知===============");        } catch (Throwable e) {            long end = System.currentTimeMillis();            if(logger.isInfoEnabled()){                logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage());            }        }    }    /**     * 用于拦截service层记录操作日志     *     * @param joinPoint 切点     */    @After("serviceAspect()")    public void after(JoinPoint joinPoint) {          /*========控制台输出=========*/        System.out.println("=====截取通知开始=====");        System.out.println("动作方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));        try {            //获取注解中对方方法的描述信息,用于service层注解            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 type = "";            String content = "";            for (Method method : methods) {                if (method.getName().equals(methodName)) {                    Class[] clazzs = method.getParameterTypes();                    if (clazzs.length == arguments.length) {                        type = method.getAnnotation(ServiceLog.class).logType();                        content = method.getAnnotation(ServiceLog.class).logContent();                        break;                    }                }            }            Log log = new Log();            log.setType(Integer.valueOf(type));            log.setContent(content);            log.setCst_create(new Date());            log.setUrl(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()");            logService.add(log);        }catch (Exception e){            e.printStackTrace();        }    }    //配置后置返回通知,使用在方法aspect()上注册的切入点    @AfterReturning("serviceAspect()")    public void afterReturn(JoinPoint joinPoint){        System.out.println("=====执行service后置返回通知=====");        if(logger.isInfoEnabled()){            logger.info("afterReturn " + joinPoint);        }    }}

在service层加上自定义的注解

 @Transactional    @Override    **@ServiceLog(logType = "1",logContent = "添加漏洞白名单")**    public ResultBO<Void> add(List<WhiteAddForm> adf,Long id){        for (int i = 0 ; i < adf.size() ; i++){            White white = new White();            WhiteAddForm whiteAddForm = adf.get(i);            white.setAssets_id(whiteAddForm.getId());            white.setVul(whiteAddForm.getName());            white.setVul_type(whiteAddForm.getType());            white.setCompany_id(id);            whiteMapper.insertSelective(white);        }        return new ResultBO<Void>(SUCCESS);    }

启动项目后debug跟踪结果图:

这里写图片描述

数据库展现:

这里写图片描述

原创粉丝点击