package com.lbx.ms.biz.xp.project.release;
import com.alibaba.fastjson.JSON;
import com.lbx.framework.common.domain.common.ResponseResult;
import com.lbx.framework.common.util.ResponseResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Objects;
@Slf4j
@Aspect
@Order(1)//指定切面的优先级
@Component
@EnableAspectJAutoProxy //使aspect起作用 自动匹配代理对象
public class TimeLogAspect {
@Pointcut("execution(* com.lbx.ms.biz.xp.project.release.service.impl.AdminToDoServiceImpl.listByPage(..)) || execution(* com.lbx.ms.biz.xp.project.release.controller.ReleaseController.getOASid(..))")//定义在Controller包里所有类,所有的方法的任意方法的执行
public void timeLog() {
}
// @Before("timeLog()")
// public void doBefore() {
// System.out.printf("Before方法执行\n");
// }
//
// @After("timeLog()")
// public void doAfter() {
// System.out.printf("after方法执行\n");
// }
@Around("timeLog()") //这里必须有返回对象,否则切面接口 void就会出现无返回
public Object logArround(ProceedingJoinPoint joinPoint){
Object proceed = null;
try {
long begin = System.currentTimeMillis();
proceed = joinPoint.proceed();
long end = System.currentTimeMillis();
long time = end - begin;
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
Object target = joinPoint.getTarget().getClass().getName();
//通过joinPoint获取参数
Object [] args = joinPoint.getArgs();
log.warn("【请求URL】{} 【总耗时】{}ms 【IP】{}",request.getRequestURL(),time,request.getRemoteAddr());
log.warn("【入参】:{}",args.length>0?JSON.toJSONString(args[0]):"无");
log.warn("【出参】:{}",JSON.toJSONString(proceed));
log.info("【调用者】:{} 【调用方法】:{}",target,joinPoint.getSignature());
return proceed;
}catch (Throwable e){
e.printStackTrace();
}finally {
return proceed;
}
}
}
aop方式记录接口耗时
最新推荐文章于 2024-03-02 17:32:00 发布