package cn.huge.module.aop; import cn.huge.base.common.utils.ObjectUtils; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; /** * @author zhouxiantao * @create 2024-08-30 11:46 */ @Component @Aspect @Slf4j public class LogAop { @Around("execution(* cn.huge.module.*.controller.*.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { long starttime = System.currentTimeMillis(); Object[] objs = joinPoint.getArgs(); try{ log.info("start to {} {}", joinPoint.getSignature().getName(), JSON.toJSONString(objs)); }catch (Exception e){ log.info("start to {} 参数不能转成字符串,忽略不处理", joinPoint.getSignature().getName()); } Object result = joinPoint.proceed(objs); long endtime = System.currentTimeMillis(); try { String resultJson = JSON.toJSONString(result); if(ObjectUtils.isNotEmpty(result) && resultJson.length() > 1000) { log.info("finish to {} 日志过长不打印 {}ms", joinPoint.getSignature().getName(),endtime-starttime); }else { log.info("finish to {} {} {}ms", joinPoint.getSignature().getName(), resultJson,endtime-starttime); } }catch (Exception e){ log.info("finish to {} 不能转成字符串,忽略不处理 {}ms", joinPoint.getSignature().getName(),endtime-starttime); } return result; } @Around("execution(* cn.huge.module.*.dao.mapper.*.*(..))") public Object aroundAdviceDao(ProceedingJoinPoint joinPoint) throws Throwable { long starttime = System.currentTimeMillis(); Object[] objs = joinPoint.getArgs(); Object result = joinPoint.proceed(objs); long endtime = System.currentTimeMillis(); Long times = endtime-starttime; if(times > 2000){ log.info("Executing sql consumes time more than 1s {} ,{}ms", joinPoint.getSignature().getName(),endtime-starttime); }else{ log.info("Executing sql consumes time {} ,{}ms", joinPoint.getSignature().getName(),endtime-starttime); } return result; } }