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

org.slf4j.impl.StaticLoggerBinder Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package org.slf4j.impl;

import de.is24.maven.slf4j.MavenPluginLoggerFactory;
import org.apache.maven.plugin.logging.Log;
import org.slf4j.ILoggerFactory;
import org.slf4j.helpers.NOPLoggerFactory;
import org.slf4j.helpers.Util;
import org.slf4j.spi.LoggerFactoryBinder;

import javax.annotation.Nonnull;

/**
 * Binds slf4j's LoggerFactory to {@link de.is24.maven.slf4j.MavenPluginLoggerFactory}.
 *
 * @author Sebastian Kirsch
 * @since 1.5
 */
public class StaticLoggerBinder implements LoggerFactoryBinder {

    private static final StaticLoggerBinder INSTANCE = new StaticLoggerBinder();

    @Nonnull
    public static StaticLoggerBinder getSingleton() {
        return INSTANCE;
    }

    private final ThreadLocal loggerFactoryForThread = new ThreadLocal();

    private StaticLoggerBinder() {
        super();
    }

    /**
     * Returns a MavenPluginLoggerFactory if the current thread was properly
     * {@link #setLog(org.apache.maven.plugin.logging.Log) initialized}; an instance of
     * NOPLoggerFactory otherwise.
     *
     * @since 1.5
     */
    @Nonnull
    @Override
    public ILoggerFactory getLoggerFactory() {
        ILoggerFactory loggerFactory = loggerFactoryForThread.get();
        if (loggerFactory == null) {
            Util.report("No Maven Log set; using NOPLoggerFactory! " +
                    "Make sure to call StaticLoggerBinder.getSingleton().setLog(Log log)!");
            loggerFactory = new NOPLoggerFactory();
        }
        return loggerFactory;
    }

    @Override
    public String getLoggerFactoryClassStr() {
        return MavenPluginLoggerFactory.class.getName();
    }

    /**
     * Sets up a MavenPluginLoggerFactory for the current thread.
     * Make sure to {@link #revokeLog() revoke the Log} after execution.
     *
     * @since 1.5
     */
    public void setLog(@Nonnull final Log log) {
        this.loggerFactoryForThread.set(new MavenPluginLoggerFactory(log));
    }

    /**
     * Tears down the MavenPluginLoggerFactory bound to the current thread.
     *
     * @since 1.5
     */
    public void revokeLog() {
        this.loggerFactoryForThread.remove();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy