package egovframework.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 * egovframework.controller..*.*(..) )")
    public Object loadingElapsedTime(ProceedingJoinPoint joinPoint) throws Throwable {

        String proceedName = String.format("%50s", joinPoint.getTarget().getClass().getSimpleName() + "." + joinPoint.getSignature().getName());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start(proceedName);

        Object proceed = joinPoint.proceed();

        stopWatch.stop();
        if (stopWatch.getTotalTimeMillis() > 1000) {
            log.warn("[API-RESULT] {}: ..END. {} ms. Completed.", proceedName, stopWatch.getTotalTimeMillis());
        }

        return proceed;
    }
}
