WebLogAspect
package ***; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.google.common.collect.Maps; 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.aspectj.lang.reflect.MethodSignature; import org.slf4j.MDC; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.Map; @Component @Aspect @Slf4j @Order public class WebLogAspect { @Pointcut("execution(* com.yinker.datacenter.web..*(..))") public void webLog(){} @Around("webLog()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { MDC.clear(); //记录请求开始执行时间: long beginTime = System.currentTimeMillis(); //获取请求信息 String methodName = pjp.getSignature().getName(); //获取请求参数: MethodSignature ms = (MethodSignature) pjp.getSignature(); //获取请求参数类型 String[] parameterNames = ms.getParameterNames(); //获取请求参数值 Object[] parameterValues = pjp.getArgs(); Map<String,Object> params = Maps.newHashMap(); for(int i=0;i<parameterNames.length;i++){ params.put(parameterNames[i],parameterValues[i]); } Object result; try { result = pjp.proceed(); } catch (Throwable throwable) { long cost = System.currentTimeMillis() - beginTime; log.info("[接口调用记录]:method:{},cost:{}ms,params:{},result:{}",methodName,cost,params,throwable.getMessage()); throw throwable; } long cost = System.currentTimeMillis() - beginTime; log.info("[接口调用记录]:method:{},cost:{}ms,params:{},result:{}",methodName,cost,params,JSON.toJSONString(result, SerializerFeature.WRITE_MAP_NULL_FEATURES)); MDC.clear(); return result; } }