package com.bear.mobile.util;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class LogAspectJ {
@Pointcut("execution(public * com.bear.mobile.*Controller.*(..))")
public void logPointCut() {
}
@Around("logPointCut()")
public Objectarount(ProceedingJoinPoint pjd)throws Throwable {
long startTime = System.currentTimeMillis();
String className = pjd.getTarget().getClass().getName();
String methodName = pjd.getSignature().getName();
Object[] args = pjd.getArgs();
try {
String params = JSON.toJSONString(args[0]);
log.info("{}.{}()【param】:{}", className, methodName, params);
}catch (Exception e) {
log.info("{}.{}()【log param fail】:{}", className, methodName, e);
}
//未做异常处理
Object result = pjd.proceed();
try {
String s = JSON.toJSONString(result);
long time = System.currentTimeMillis() - startTime;
log.info("{}.{}()【result】:{} 【执行时长为】:{}{}", className, methodName, s, time," ms" );
}catch (Exception e) {
log.info("{}.{}()【log result fail】:{} ", className, methodName, e);
}
return result;
}
}