Aop+自定义注解实现日志插入
1.自定义注解AutoApiLog
package org.jeecg.modules.dataark.aspect.annotation;
import java.lang.annotation.*;
/**
* 系统日志注解
*
* @Author scott
* @email jeecgos@163.com
* @Date 2019年1月14日
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AutoApiLog {
/**
* 调用接口编码
*
* @return
*/
String apiCode() default "";
/**
* 业务类型编码
*
* @return
*/
String businessCode() default "";
/**
* 业务类型名称
*
* @return
*/
String businessName() default "";
2.自定义Aop切面
```java
package org.jeecg.modules.dataark.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
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.aspectj.lang.reflect.MethodSignature;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.constart.OperateConstant;
import org.jeecg.modules.dataark.aspect.annotation.AutoApiLog;
import org.jeecg.common.operate.entity.YwDataarkApiLog;
import org.jeecg.modules.dataark.service.IYwDataarkApiLogService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Date;
/**
* DataArk接口调用日志,切面处理类
*
* @Author scott
* @email jeecgos@163.com
* @Date 2018年1月14日
*/
@Slf4j
@Aspect
@Component
public class AutoApiLogAspect {
@Resource
private IYwDataarkApiLogService apiLogService;
@Pointcut("@annotation(org.jeecg.modules.dataark.aspect.annotation.AutoApiLog)")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
//long beginTime = System.currentTimeMillis();
// 接口开始执行时间
Date startTime=new Date();
//执行方法
Object result = point.proceed();
// 接口执行完成时间
Date endTime=new Date();
//执行时长(毫秒)
//long time = System.currentTimeMillis() - beginTime;
//保存日志
saveSysLog(point,result,startTime,endTime);
return result;
}
private void saveSysLog(ProceedingJoinPoint joinPoint, Object result,Date startTime,Date endTime) throws NoSuchMethodException {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
YwDataarkApiLog dto = new YwDataarkApiLog();
AutoApiLog syslog = method.getAnnotation(AutoApiLog.class);
if(syslog != null){
//update-begin-author:taoyan date:
// 调用接口编码
String apiCode=syslog.apiCode();
// 业务类型编码
String businessCode=syslog.businessCode();
// 业务类型名称
String businessName=syslog.businessName();
//注解上的描述,操作日志内容
dto.setApiCode(apiCode);
dto.setBusinessCode(businessCode);
dto.setBusinessName(businessName);
}
// 获取请求参数
JSONObject params=myAdvice(joinPoint);
String paramsString=JSONObject.toJSONString(params);
dto.setApiParam(paramsString);
// 获取业务id
String businessId=params.getString("businessId");
dto.setBusinessId(businessId);
try {
//Object object=joinPoint.proceed();
//获取方法返回值处理
String objectString=JSONObject.toJSONString(result);
dto.setApiReturn(objectString);
log.info("返回值="+result);
dto.setApiStatus(OperateConstant.FAIL);
if(ObjectUtils.isNotEmpty(result) ){
JSONObject res= JSON.parseObject(objectString);
if("success".equals(res.getString("result"))){
dto.setApiStatus(OperateConstant.SUCCESS);
}
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
//获取登录用户信息
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
if(sysUser!=null){
dto.setSysOrgCode(sysUser.getOrgCode());
dto.setCreateBy(sysUser.getUsername());
dto.setUpdateBy(sysUser.getUsername());
}
dto.setStartTime(startTime);
dto.setEndTime(endTime);
//保存日志
apiLogService.saveData(dto);
}
public JSONObject myAdvice(JoinPoint joinPoint) {
JSONObject json=new JSONObject();
Object[] args = joinPoint.getArgs(); // 获取参数列表
Method method = ((MethodSignature)joinPoint.getSignature()).getMethod(); // 获取目标方法
Parameter[] parameters = method.getParameters(); // 获取方法的参数信息
for (int i=0; i<args.length; i++) {
String paramName = parameters[i].getName(); // 获取参数名
log.info("参数名:" + paramName);
log.info("参数值:" + args[i]);
json.put(paramName,args[i]);
}
return json;
}
}
}
3.业务方法调用
```java
package org.jeecg.modules.common.component;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.tripartite.api.ITripartiteBaseAPI;
import org.jeecg.common.tripartite.vo.dataArk.MrsDatabaseSchemaVo;
import org.jeecg.common.tripartite.vo.dataArk.PrivilegesDwsGussdbParam;
import org.jeecg.common.tripartite.vo.dataArk.SyncLinkVo;
import org.jeecg.modules.common.config.DataArkConfig;
import org.jeecg.modules.dataark.aspect.annotation.AutoApiLog;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 开发场景调用dataark接口日志记录
*/
@Component
@Slf4j
public class SceneApiComponent {
@Resource
public ITripartiteBaseAPI tripartiteBaseAPI;
@AutoApiLog(apiCode="scene_empower_createWorkspaces",businessCode="scene_empower",businessName="场景授权")
public JSONObject apiWorkspacesAdd(String businessId, String name, String desc) {
// 具体实现逻辑
return null;
}
}