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.Util;
029import org.slf4j.spi.SLF4JServiceProvider;
030
031/**
032 * MarkerFactory is a utility class producing {@link Marker} instances as
033 * appropriate for the logging system currently in use.
034 * 
035 * <p>
036 * This class is essentially implemented as a wrapper around an
037 * {@link IMarkerFactory} instance bound at compile time.
038 * 
039 * <p>
040 * Please note that all methods in this class are static.
041 * 
042 * @author Ceki G&uuml;lc&uuml;
043 */
044public class MarkerFactory {
045    static IMarkerFactory MARKER_FACTORY;
046
047    private MarkerFactory() {
048    }
049
050    // this is where the binding happens
051    static {
052        SLF4JServiceProvider provider = LoggerFactory.getProvider();
053        if (provider != null) {
054            MARKER_FACTORY = provider.getMarkerFactory();
055        } else {
056            Util.report("Failed to find provider");
057            Util.report("Defaulting to BasicMarkerFactory.");
058            MARKER_FACTORY = new BasicMarkerFactory();
059        }
060    }
061
062    /**
063     * Return a Marker instance as specified by the name parameter using the
064     * previously bound {@link IMarkerFactory}instance.
065     * 
066     * @param name
067     *          The name of the {@link Marker} object to return.
068     * @return marker
069     */
070    public static Marker getMarker(String name) {
071        return MARKER_FACTORY.getMarker(name);
072    }
073
074    /**
075     * Create a marker which is detached (even at birth) from the MarkerFactory.
076     *
077     * @param name the name of the marker
078     * @return a dangling marker
079     * @since 1.5.1
080     */
081    public static Marker getDetachedMarker(String name) {
082        return MARKER_FACTORY.getDetachedMarker(name);
083    }
084
085    /**
086     * Return the {@link IMarkerFactory}instance in use.
087     * 
088     * <p>The IMarkerFactory instance is usually bound with this class at 
089     * compile time.
090     * 
091     * @return the IMarkerFactory instance in use
092     */
093    public static IMarkerFactory getIMarkerFactory() {
094        return MARKER_FACTORY;
095    }
096}