package com.zpc.tet.controller.log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @Author timor
* @Date 2019/8/23
*
* 统一日志格式
*/
@Aspect
@Component
public class SysLogAspect {
@Pointcut("@annotation(com.zpc.tet.controller.log.Log)")
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;
}
@AfterReturning("logPointCut()")
public void after(JoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
System.out.println(method.getAnnotation(Log. class));
String className = point.getTarget().getClass().getName();
String methodName = signature.getName();
System.out.println(className + "." + methodName + "()");
}
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println(method.getAnnotation(Log.class));
//请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
System.out.println(className + "." + methodName + "()");
//请求的参数
Object[] args = joinPoint.getArgs();
System.out.println(args[0]);
}
}
还没有评论,来说两句吧...