package cn.palmte.work.config.log;

import cn.palmte.work.utils.StrKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 日志打印排除器，排除某些日志的打印
 * @author xiongshiyan at 2018/12/23 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@Component
public class LogExcluder implements InitializingBean{
    private static final Logger logger = LoggerFactory.getLogger(LogExcluder.class);

    @Value("${server.context-path:/fourcal}")
    private String contextPath;
    @Value("${fourcal.log.excluded.urls:}")
    private String[] logExcludedUrls;

    @Override
    public void afterPropertiesSet() throws Exception {
        if(isEmpty(logExcludedUrls)){
            return;
        }
        StrKit.trim(logExcludedUrls);
        logger.info(Arrays.asList(logExcludedUrls).toString());
    }

    public boolean needExcluded(String uri){
        //没有需要排除的
        if(isEmpty(logExcludedUrls)){
            return false;
        }

        //去除前端轮询的
        if(uri.contains("api/pc/alarm/fetchAlarm")){
            return true;
        }

        //去掉前缀
        uri = uri.replace(contextPath , "");
        return StrKit.targetInArray(logExcludedUrls , uri);
    }

    private <T> boolean isEmpty(T[] array){
        return null == array || 0 == array.length;
    }
}
