日志记录切面技术代码

注解类:


@Retention(RetentionPolicy.RUNTIME)
//元注解,定义注解被保留策略,一般有三种策略
//1、RetentionPolicy.SOURCE 注解只保留在源文件中,在编译成class文件的时候被遗弃
//2、RetentionPolicy.CLASS 注解被保留在class中,但是在jvm加载的时候北欧抛弃,这个是默认的声明周期
//3、RetentionPolicy.RUNTIME 注解在jvm加载的时候仍被保留
@Target({ElementType.METHOD}) //定义了注解声明在哪些元素之前
@Documented
public @interface SystemControllerLog {
    //定义成员
    String descrption() default "" ;//描述
    String actionType() default "" ;//操作的类型,1、添加 2、修改 3、删除 4、查询
}

日志切面以及处理类:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Aspect
@Component
@Order(-5)
public class SystemLogAspect {

    @Autowired
    private SystemLogService systemLogService;

    /***
     * 定义controller切入点拦截规则,拦截SystemControllerLog注解的方法
     */
    @Pointcut("@annotation(com.sunwin.ebs.admin.web.common.SystemControllerLog)")
    public void controllerAspect(){}

    /***
     * 拦截控制层的操作日志
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("controllerAspect()")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {
        SystemLog systemLog = new SystemLog();
        Object proceed = null ;
        //获取session中的用户
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Users sysUser = (Users)request.getSession().getAttribute("loginInfo");
        systemLog.setUserId(sysUser.getId());
        systemLog.setUserName(sysUser.getUsername());
        //获取请求的ip
        String ip = request.getRemoteAddr();
        systemLog.setIp(ip);
        systemLog.setIsSuccess("1");
        systemLog.setEvent(getControllerMethodDescription(joinPoint));
        systemLogService.saveSystemLog(systemLog);
        proceed = joinPoint.proceed();
        return proceed ;
    }

    //异常处理
    @AfterThrowing(pointcut = "controllerAspect()",throwing="e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) throws Throwable{
        SystemLog systemLog = new SystemLog();
        Object proceed = null ;
        //获取session中的用户
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Users sysUser = (Users)request.getSession().getAttribute("loginInfo");
        systemLog.setUserId(sysUser.getId());
        systemLog.setUserName(sysUser.getUsername());
        //获取请求的ip
        String ip = request.getRemoteAddr();
        systemLog.setIp(ip);
        systemLog.setIsSuccess("2");
        systemLog.setEvent(getControllerMethodDescription((ProceedingJoinPoint) joinPoint));
        systemLogService.saveSystemLog(systemLog);
    }


    /***
     * 获取controller的操作信息
     * @param point
     * @return
     */
    public String getControllerMethodDescription(ProceedingJoinPoint point) throws  Exception{
        //获取连接点目标类名
        String targetName = point.getTarget().getClass().getName() ;
        //获取连接点签名的方法名
        String methodName = point.getSignature().getName() ;
        //获取连接点参数
        Object[] args = point.getArgs() ;
        //根据连接点类的名字获取指定类
        Class targetClass = Class.forName(targetName);
        //获取类里面的方法
        Method[] methods = targetClass.getMethods() ;
        String description="" ;
        for (Method method : methods) {
            if (method.getName().equals(methodName)){
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == args.length){
                    description = method.getAnnotation(SystemControllerLog.class).descrption();
                    break;
                }
            }
        }
        return description ;
    }
}

使用方法:

@SystemControllerLog(descrption = "修改信息")
    @RequestMapping(value = R.ajax_modifyMealShop, method = RequestMethod.POST)
    @ResponseBody
    public String ajax_modifyMealShop(HttpServletRequest request, Delicacy delicacy) throws Exception {
    }

你可能感兴趣的:(java,日志管理,Logger,java)