import com.alibaba.fastjson.JSON;
import com.megvii.performance.common.exception.CommonException;
import com.megvii.performance.common.utils.SystemUtil;
import com.megvii.performance.pojo.entity.sys.OperateLogDO;
import com.megvii.performance.pojo.entity.sys.SysUserDO;
import com.megvii.performance.service.sys.OperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
@Aspect
@Component
@Slf4j
public class OperateLogAspect {
private OperateLogService logService;
@Autowired
public OperateLogAspect(OperateLogService logService) {
this.logService = logService;
}
@Pointcut(value = "@annotation(com.megvii.performance.common.annotation.OperateLog)")
public void pointCut() {
}
@Around(value = "pointCut()")
public Object operateLog(ProceedingJoinPoint pjp) {
String operateUser;
SysUserDO sysUserDO = ((SysUserDO)SecurityUtils.getSubject().getPrincipal());
if (null == sysUserDO){
operateUser = "client";
}else {
operateUser = sysUserDO.getUserId();
}
OperateLogDO operateLog = new OperateLogDO();
operateLog.setOperateDate(new Date());
operateLog.setOperateUserId(operateUser);
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
operateLog.setRequestIp(SystemUtil.getRealIP(request));
operateLog.setRequestUrl(request.getRequestURL().toString());
Object[] args = Arrays.stream(pjp.getArgs()).filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof MultipartFile) && !(arg instanceof HttpServletResponse))).toArray();
operateLog.setRequestParams(JSON.toJSONString(args));
long startTime = System.currentTimeMillis();
try {
Object ret = pjp.proceed(pjp.getArgs());
long endTime = System.currentTimeMillis();
String result = JSON.toJSONString(ret);
operateLog.setResponseResult(result);
operateLog.setTimeConsuming((int) (endTime - startTime));
logService.save(operateLog);
return ret;
} catch (Throwable throwable) {
long endTime = System.currentTimeMillis();
String result = JSON.toJSONString(throwable.getMessage());
operateLog.setResponseResult(result);
operateLog.setTimeConsuming((int) (endTime - startTime));
logService.save(operateLog);
log.error("执行{}方法出错: {}, {}", pjp.getSignature().toString(), throwable.getMessage(), throwable.getStackTrace());
if (throwable.getClass().equals(CommonException.class)){
throw new CommonException(throwable.getMessage(), throwable.getCause());
}else if(throwable instanceof IllegalArgumentException){
throw new IllegalArgumentException(throwable.getMessage(), throwable.getCause());
}else {
throw new RuntimeException(throwable.getMessage(), throwable.getCause());
}
}
}
}
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperateLog {
String remark() default "";
String operType() default "";
}