不废话了,直接上代码
/**
* 打印详细的参数名称、参数类型、参数值
*/
@Component
@Aspect
@Slf4j
public class LogServiceAspect {
@Pointcut("execution(* com.upgrade.biz.service..*(..))")
public void serviceLog(){}
//
// @Before("serviceLog()")
// public void beforeServiceLog(JoinPoint joinPoint){
// timeStart = System.currentTimeMillis();
// log.info("前置通知");
// }
//
// @After("serviceLog()")
// public void afterServiceLog(JoinPoint joinPoint){
// String name = joinPoint.getSignature().getName();
// long timeEnd = System.currentTimeMillis();
// log.info("后置通知,方法名字:{},方法执行时间:{}",name,timeEnd-timeEnd);
// }
//
// @AfterReturning("serviceLog()")
// public void afterReturning(JoinPoint joinPoint){
// log.info("返回结果以后");
// }
//
// @AfterThrowing("serviceLog()")
// public void afterThrowing(JoinPoint joinPoint){
// log.info("返回异常");
// }
//
@Around("serviceLog()")
public Object serviceLogAround(ProceedingJoinPoint point){
String className = point.getTarget().getClass().getName();
String methodName = point.getSignature().getName();
try {
long start = System.currentTimeMillis();
Object result = point.proceed();
long runTime = System.currentTimeMillis() - start;
log.info("class:{},method:{},args:{},runTime:{},result:{}",className,methodName, handlerParameter(point),runTime, JSON.toJSONString(result));
return result;
} catch (RuntimeException e){
e.printStackTrace();
return Results.failure("系统开小差了");
} catch (Throwable throwable) {
throwable.printStackTrace();
return Results.failure("系统异常");
}
}
private String handlerParameter(ProceedingJoinPoint point){
StringBuilder stringBuilder = new StringBuilder();
MethodSignature methodSignature = (MethodSignature)point.getSignature();
String[] parameterNames = methodSignature.getParameterNames();
Class[] parameterTypes = methodSignature.getParameterTypes();
Object[] args = point.getArgs();
int i=0;
for (Object pojo : args){
stringBuilder.append("parameterName:").append(parameterNames[i]);
stringBuilder.append("parameterType:").append(parameterTypes[i]);
stringBuilder.append("parameterValue:").append(pojo);
}
return stringBuilder.toString();
}
}
亲测可用,有问题,可以留言,我看到就会回复