001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j;
026
027import org.slf4j.helpers.BasicMarkerFactory;
028import org.slf4j.helpers.Reporter;
029import org.slf4j.helpers.Util;
030import org.slf4j.spi.SLF4JServiceProvider;
031
032/**
033 * MarkerFactory is a utility class producing {@link Marker} instances as
034 * appropriate for the logging system currently in use.
035 * 
036 * <p>
037 * This class is essentially implemented as a wrapper around an
038 * {@link IMarkerFactory} instance bound at compile time.
039 * 
040 * <p>
041 * Please note that all methods in this class are static.
042 * 
043 * @author Ceki G&uuml;lc&uuml;
044 */
045public class MarkerFactory {
046    static IMarkerFactory MARKER_FACTORY;
047
048    private MarkerFactory() {
049    }
050
051    // this is where the binding happens
052    static {
053        SLF4JServiceProvider provider = LoggerFactory.getProvider();
054        if (provider != null) {
055            MARKER_FACTORY = provider.getMarkerFactory();
056        } else {
057            Reporter.error("Failed to find provider");
058            Reporter.error("Defaulting to BasicMarkerFactory.");
059            MARKER_FACTORY = new BasicMarkerFactory();
060        }
061    }
062
063    /**
064     * Return a Marker instance as specified by the name parameter using the
065     * previously bound {@link IMarkerFactory}instance.
066     * 
067     * @param name
068     *          The name of the {@link Marker} object to return.
069     * @return marker
070     */
071    public static Marker getMarker(String name) {
072        return MARKER_FACTORY.getMarker(name);
073    }
074
075    /**
076     * Create a marker which is detached (even at birth) from the MarkerFactory.
077     *
078     * @param name the name of the marker
079     * @return a dangling marker
080     * @since 1.5.1
081     */
082    public static Marker getDetachedMarker(String name) {
083        return MARKER_FACTORY.getDetachedMarker(name);
084    }
085
086    /**
087     * Return the {@link IMarkerFactory}instance in use.
088     * 
089     * <p>The IMarkerFactory instance is usually bound with this class at 
090     * compile time.
091     * 
092     * @return the IMarkerFactory instance in use
093     */
094    public static IMarkerFactory getIMarkerFactory() {
095        return MARKER_FACTORY;
096    }
097}