All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.logging.LogFactory Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
package liquibase.logging;

import liquibase.exception.ServiceNotFoundException;
import liquibase.logging.core.DefaultLogger;
import liquibase.servicelocator.ServiceLocator;

import java.util.HashMap;
import java.util.Map;

public class LogFactory {
    private final Map loggers = new HashMap();
    private static String defaultLoggingLevel = null;
    private static DefaultLogger defaultLogger = new DefaultLogger();

    private static LogFactory instance;

    public static synchronized void reset() {
        instance = new LogFactory();
    }

    public static synchronized LogFactory getInstance() {
        if (instance == null) {
            instance = new LogFactory();
        }
        return instance;
    }

    /**
     * Set the instance used by this singleton. Used primarily for testing.
     */
    public static void setInstance(LogFactory instance) {
        LogFactory.instance = instance;
    }

    /**
     * @deprecated Use non-static {@link #getLog(String)} method
     */
    public static Logger getLogger(String name) {
        return getInstance().getLog(name);
    }

    public Logger getLog(String name) {
        if (!loggers.containsKey(name)) {
            Logger value;
            try {
                ServiceLocator serviceLocator = ServiceLocator.getInstance();
                if (serviceLocator == null) {
                    return defaultLogger; //ServiceLocator not yet running
                }
                value = (Logger) serviceLocator.newInstance(Logger.class);
            } catch (Exception e) {
                return defaultLogger;
            }
            value.setName(name);
            if (defaultLoggingLevel != null) {
                value.setLogLevel(defaultLoggingLevel);
            }
            loggers.put(name, value);
        }

        return loggers.get(name);
    }

   /**
     * @deprecated Use non-static {@link #getLog()} method
     */
    public static Logger getLogger() {
        return getInstance().getLog();
    }

    public Logger getLog() {
        return getLog("liquibase");
    }

    public void setDefaultLoggingLevel(String defaultLoggingLevel) {
        this.defaultLoggingLevel = defaultLoggingLevel;
    }

    public void setDefaultLoggingLevel(LogLevel defaultLoggingLevel) {
        this.defaultLoggingLevel = defaultLoggingLevel.name();
    }

    /**
     * @deprecated Use non-static {@link #setDefaultLoggingLevel(String)} method
     */
    public static void setLoggingLevel(String defaultLoggingLevel) {
        getInstance().defaultLoggingLevel = defaultLoggingLevel;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy