package com.yutou.qqbot.utlis;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.StackLocatorUtil;


public class Log {

    public static void i() {
        System.out.println();
    }

    public static Logger getDynamicLogger(String loggerName) {
        return DynamicLogFile.getLogger(loggerName);
    }

    public static void removeDynamicLogger(String loggerName) {
        DynamicLogFile.remove(loggerName);
    }

    public static void i(Object... log) {
        if (!((boolean) ConfigTools.load(ConfigTools.CONFIG, ConfigTools.SERVICE_LOG))) {
            return;
        }

        LogManager.getLogger(getStackTrace()).info(buildLog(log));
    }

    public static void e(Object... log) {
        if (!ConfigTools.load(ConfigTools.CONFIG, ConfigTools.SERVICE_LOG, Boolean.class)) {
            return;
        }
        LogManager.getLogger(getStackTrace()).error(buildLog(log));
    }

    public static void e(Throwable e, Object... log) {
        if (!ConfigTools.load(ConfigTools.CONFIG, ConfigTools.SERVICE_LOG, Boolean.class)) {
            return;
        }
        LogManager.getLogger(getStackTrace()).error(buildLog(log), e);
    }

    public static void e(Throwable e) {
        if (!ConfigTools.load(ConfigTools.CONFIG, ConfigTools.SERVICE_LOG, Boolean.class)) {
            return;
        }
        LogManager.getLogger().error(getStackTrace(), e);
    }


    public static void d(Object... log) {
        if (!ConfigTools.load(ConfigTools.CONFIG, ConfigTools.SERVICE_LOG, Boolean.class)) {
            return;
        }
        LogManager.getLogger().debug(buildLog(log));
    }

    private static String getStackTrace() {
        StackTraceElement element = StackLocatorUtil.getStackTraceElement(3);
        return "(" + element.getFileName() + ":" + element.getLineNumber() + ")";
    }

    private static String buildLog(Object... log) {
        StringBuilder sb = new StringBuilder();
        for (Object obj : log) {
            if (!sb.isEmpty()) {
                sb.append("|");
            }
            sb.append(obj);
        }
        return sb.toString();
    }
}
