開發時請求數據顯示

/**
 * 開發時請求數據顯示
 * @author xxm
 * @date 2019/1/5 11:16
 * @version V1.0
 */
@Aspect
@Component
@ConditionalOnProperty(name = "dev.webLog", havingValue = "true")
public class WebLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    //掃描controller中的請求
    @Pointcut("execution( * *..controller.*.*(..))")
    public void logPointCut() {
    }

    /**
     * 顯示請求時數據
     */
    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到請求,記錄請求內容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 記錄下請求內容
        logger.debug(request.getMethod()+" 請求 : " + request.getRequestURL().toString());
        // 獲取真實的ip地址
//        logger.info("IP : " + IPAddressUtil.getClientIpAddress(request));
        logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."
                + joinPoint.getSignature().getName());
        logger.debug("參數 : " + Arrays.toString(joinPoint.getArgs()));
//        loggger.info("參數 : " + joinPoint.getArgs());

    }

    /**
     * 顯示請求完成的返回值
     * returning的值和doAfterReturning的參數名一致
     */
    @AfterReturning(returning = "ret", pointcut = "logPointCut()")
    public void doAfterReturning(Object ret) {
        // 處理完請求,返回內容(返回值太複雜時,打印的是物理存儲空間的地址)
        logger.debug("返回值 : " + ret);
    }

    /**
     * 顯示請求耗時
     */
    @Around("logPointCut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis();
        // ob 爲方法的返回值
        Object ob = pjp.proceed();
        logger.debug("耗時 : " + (System.currentTimeMillis() - startTime));
        return ob;
    }
}
相關文章
相關標籤/搜索