一、interface
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String value() default "";
}
二、Aspect
@Aspect
@Component
@Order()
public class SysLogAspect {
@Pointcut("@annotation(com.***.SysLog)")
public void logPointCut() {
}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
Object result = point.proceed();
long time = System.currentTimeMillis() - beginTime;
saveSysLog(point, time);
return result;
}
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SysLogDo sysLog = new SysLogDo();
SysLog syslog = method.getAnnotation(SysLog.class);
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
sysLog.setMethod(className + "." + methodName + "()");
Object[] args = joinPoint.getArgs();
try{
String params = JSON.toJSONString(args[0]);
sysLog.setParams(params);
sysLog.setContent(syslog.value()+":"+params);
}catch (Exception e){
}
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
sysLog.setIp(IPUtils.getIpAddr(request));
}
}
二、Controller
@RestController
@RequestMapping("/R")
public class RController {
@SysLog("新增R")
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public BaseResponse addR(@RequestBody RFrom rFrom) {
}
}