package com.its.api.aop;

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;
import org.springframework.util.StopWatch;

@Slf4j
@Aspect
@Component
public class LoggingAspect {

    @Around("execution( public * com.its.op.controller..*.*(..) )")
    public Object loadingElapsedTime(ProceedingJoinPoint joinPoint) throws Throwable {

        String proceedName = String.format("%50s", joinPoint.getTarget().getClass().getSimpleName() + "." + joinPoint.getSignature().getName());
        log.info("[LOADING...] {}: START.", proceedName);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start(proceedName);

        Object proceed = joinPoint.proceed();

        stopWatch.stop();
        log.info("[LOADING...] {}: ..END. {} ms. Completed.", proceedName, stopWatch.getTotalTimeMillis());

        return proceed;
    }
}
