切面Controller出入参日志打印
项目结构
切面日志对controller下所有的方法生效
切面代码
@Slf4j
@Aspect
@Component
public class ControllerLogAspect {
// 定义一个切点,拦截所有Controller层的public方法
@Before("execution(public * com.jzt.market.controller..*.*(..))")
public void logBefore(JoinPoint joinPoint) throws NoSuchMethodException {
// 获取方法名
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getName();
Object target = joinPoint.getTarget();
Method method = target.getClass().getMethod(signature.getName(), signature.getParameterTypes());
Operation annotation = method.getAnnotation(Operation.class);
String summary = annotation.summary();
// 获取入参
Object[] args = joinPoint.getArgs();
log.info("进入[{}]方法:=>{},入参:{}", summary, methodName,args);
}
@AfterReturning(pointcut = "execution(public * jx.mim.market.controller..*.*(..))", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
// 获取方法名
String methodName = joinPoint.getSignature().getName();
log.info("结束方法:{},出参:{}", methodName, JsonUtils.toJsonPrettyString(result));
}
}