package cn.axzo.workflow.starter.handler.execute.interceptor;

import cn.axzo.workflow.starter.handler.execute.ListenerExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;

import java.util.function.Consumer;

public final class LogInterceptor extends AbstractListenerInterceptor {
    private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class);

    @Override
    public <T> void execute(ListenerExecutor executor, Consumer<T> consumer, T t) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.debug("--- starting {} ----", executor.getClass().getSimpleName());
        try {
            getNext().execute(executor, consumer, t);
        } finally {
            stopWatch.stop();
            log.debug("--- {} finished ,timeCost:{} ms ---", executor.getClass().getSimpleName(), stopWatch.getTotalTimeMillis());
        }
    }
}
