package sample;

import java.util.HashMap;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by sebastian on 08.05.17.
 */
@SuppressWarnings("WeakerAccess")
public class Log {
    private static final Level LOG_LEVEL = Level.FINEST;


    private static HashMap<String, Logger> logger = new HashMap<>();
    private static ConsoleHandler consoleHandler;

    public static Logger getLogger(String key) {
        if (!logger.containsKey(key)) {
            Logger newLogger = Logger.getLogger(key);
            newLogger.setLevel(LOG_LEVEL);
            newLogger.addHandler(getConsoleHandler());
            logger.put(key, newLogger);
        }

        return logger.get(key);
    }

    private static ConsoleHandler getConsoleHandler() {
        if (consoleHandler == null) {
            consoleHandler = new ConsoleHandler();
            consoleHandler.setLevel(Level.FINEST);
        }
        return consoleHandler;
    }

    public static void finer(String key, String log) {
        getLogger(key).log(Level.FINER, log);
    }

    public static void finer(String key, int log) {
        getLogger(key).log(Level.FINER, String.valueOf(log));
    }

    public static void finer(String key, boolean log) {
        getLogger(key).log(Level.FINER, String.valueOf(log));
    }

    public static void severe(String key, String log) {
        getLogger(key).log(Level.SEVERE, log);
    }

    public static void severe(String key, int log) {
        getLogger(key).log(Level.SEVERE, String.valueOf(log));
    }

    public static void severe(String key, boolean log) {
        getLogger(key).log(Level.SEVERE, String.valueOf(log));
    }
}
