@Slf4j @Aspect @Component public class LogAspect { @Pointcut("execution(* com.sankuai.credit.caselibrary.server.service.impl.*.*(..))") private void service() { } @Around("service()||@annotation(com.sankuai.credit.caselibrary.server.aop.Log)") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { String methodInvokeLog = buildMethodInvokeLog(joinPoint); try { Object result = joinPoint.proceed(); log.info("[LogAspect]{}=>{}", methodInvokeLog, CommonUtils.toJsonString(result)); return result; } catch (Throwable e) { log.error("[LogAspect]{}=>fail", methodInvokeLog, e); throw e; } } private String buildMethodInvokeLog(ProceedingJoinPoint joinPoint) { StringBuilder sb = new StringBuilder(); String className = joinPoint.getTarget().getClass().getSimpleName(); String method = joinPoint.getSignature().getName(); sb.append(className + "." + method + "("); Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { sb.append(args[i]); if (i != args.length - 1) { sb.append(","); } } sb.append(")"); return sb.toString(); } }
切面打日志
最新推荐文章于 2024-11-14 13:33:46 发布