SpringAOP日志切面处理
一、创建自定义注解
/**
* @Author ***
* @Date 2022/1/4 9:15
* @Version 1.0
* @remark 系统日志,切面处理类
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AutoLog {
/**
* 操作风险类型(1:高、2:低)
*/
int operateRiskType() default 0;
/**
* 操作分类类型(1:系统、2:业务)
*/
int operateClassifyType() default 0;
/**
* 日志操作内容
*/
String operateContent();
}
二、创建切面处理类
/**
* @Author ***
* @Date 2022/1/4 9:15
* @Version 1.0
* @remark 系统日志,切面处理类
*/
@Component//加入IOC容器
@Aspect//表示一个切面
public class AutoLogAspect {
@Resource
private UavLogOperationMapper uavLogOperationMapper;
/**
* 日志对象
*/
private final static Logger logger = LoggerFactory.getLogger(AutoLogAspect.class);
/**
* 自定义切入点(路径指向 AutoLog类)
*/
@Pointcut("@annotation(net.jointflight.project.blog.AutoLog)")
public void logPointCut() {
}
/**
* 在每个日志横切面之前执行
*/
@Before(value = "logPointCut()")
public void doBefore() {
logger.info("===================== Start【记录日志】 =======================");
}
/**
* 环绕记录日志
*
* @param joinPoint
* @throws Throwable
*/
@Around("logPointCut()")
public ResultData<Object> doBefore(ProceedingJoinPoint joinPoint) {
ResultData<Object> resultData = null;
try {
//得到Request对象
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
UavUser uavUser =null;
//获取登录用户信息
try {
//根据自己系统获取登录session方式
uavUser = AuthUtil.newfilter(UavUserType.工程管理员, UavUserType.超级管理员);
}catch (UnAuthorizedException unAuthorizedException){
unAuthorizedException.printStackTrace();
return ResultData.error("用户登录信息过期");
}
//请求拦截
resultData = (ResultData<Object>) joinPoint.proceed();
//日志记录对象
UavLogOperation uavLogOperation = getAspectLogDescription(joinPoint);
uavLogOperation.setLogId(System.currentTimeMillis() + "_" + (int) ((Math.random() * 9 + 1) * 1000));
//用户操作IP
uavLogOperation.setOperationIp(request.getRemoteAddr());
//用户操作ID
uavLogOperation.setOperationId(uavUser.getUserId());
//用户操作名称
uavLogOperation.setOperationName(uavUser.getName());
//状态
if (resultData.getCode() != 1) {
uavLogOperation.setState(0);
} else {
uavLogOperation.setState(1);
}
//状态结果
uavLogOperation.setStateResult(resultData.getMsg_customer());
//请求地址
uavLogOperation.setRequestUrl(request.getRequestURL().toString());
//记录时间
uavLogOperation.setLogTime(new Date());
logger.info(uavLogOperation.toString());
uavLogOperationMapper.insert(uavLogOperation);
} catch (Exception e) {
e.printStackTrace();
return ResultData.error("操作日志记录错误");
} catch (Throwable throwable) {
throwable.printStackTrace();
return ResultData.error("操作日志记录错误");
}
logger.info("===================== End【记录日志】 =======================");
return resultData;
}
/**
* 获取切面注解的描述
*
* @param joinPoint 切点
* @return 描述信息
* @throws Exception
*/
public UavLogOperation getAspectLogDescription(JoinPoint joinPoint)
throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
//日志记录对象
UavLogOperation uavLogOperation = new UavLogOperation();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazz = method.getParameterTypes();
if (clazz.length == arguments.length) {
//日志描述
uavLogOperation.setContent(method.getAnnotation(AutoLog.class).operateContent());
//风险等级
uavLogOperation.setRiskGrade(method.getAnnotation(AutoLog.class).operateRiskType());
//业务分类
uavLogOperation.setClassify(method.getAnnotation(AutoLog.class).operateClassifyType());
break;
}
}
}
return uavLogOperation;
}
}
三、定义常量类-方便后期如果有需要修改类型参数
/**
* @Author ***
* @Date 2022/1/6 9:29
* @Version 1.0
* @remark 类型全局定义
*/
public class AutoType {
/** 风险等级 1:高 */
public final static int LOG_RISK_TYPE_1=1;
/** 风险等级 2:低 */
public final static int LOG_RISK_TYPE_2=2;
/** 业务分类 1:系统 */
public final static int LOG_CLASSIFY_TYPE_1=1;
/** 风险等级 2:业务 */
public final static int LOG_CLASSIFY_TYPE_2=2;
}
四、指向切入点-例:写在controller层