Source code

    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.impl.StaticMarkerBinder;
    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    /**
    051     * As of SLF4J version 1.7.14, StaticMarkerBinder classes shipping in various bindings
    052     * come with a getSingleton() method. Previously only a public field called SINGLETON
    053     * was available.
    054     *
    055     * @return IMarkerFactory
    056     * @throws NoClassDefFoundError in case no binding is available
    057     * @since 1.7.14
    058     */
    059    private static IMarkerFactory bwCompatibleGetMarkerFactoryFromBinder() throws NoClassDefFoundError {
    060        try {
    061            return StaticMarkerBinder.getSingleton().getMarkerFactory();
    062        } catch (NoSuchMethodError nsme) {
    063            // binding is probably a version of SLF4J older than 1.7.14
    064            return StaticMarkerBinder.SINGLETON.getMarkerFactory();
    065        }
    066    }
    067
    068    // this is where the binding happens
    069    static {
    070        try {
    071            MARKER_FACTORY = bwCompatibleGetMarkerFactoryFromBinder();
    072        } catch (NoClassDefFoundError e) {
    073            MARKER_FACTORY = new BasicMarkerFactory();
    074        } catch (Exception e) {
    075            // we should never get here
    076            Util.report("Unexpected failure while binding MarkerFactory", e);
    077        }
    078    }
    079
    080    /**
    081     * Return a Marker instance as specified by the name parameter using the
    082     * previously bound {@link IMarkerFactory}instance.
    083     *
    084     * @param name
    085     *          The name of the {@link Marker} object to return.
    086     * @return marker
    087     */
    088    public static Marker getMarker(String name) {
    089        return MARKER_FACTORY.getMarker(name);
    090    }
    091
    092    /**
    093     * Create a marker which is detached (even at birth) from the MarkerFactory.
    094     *
    095     * @param name the name of the marker
    096     * @return a dangling marker
    097     * @since 1.5.1
    098     */
    099    public static Marker getDetachedMarker(String name) {
    100        return MARKER_FACTORY.getDetachedMarker(name);
    101    }
    102
    103    /**
    104     * Return the {@link IMarkerFactory}instance in use.
    105     *
    106     * <p>The IMarkerFactory instance is usually bound with this class at
    107     * compile time.
    108     *
    109     * @return the IMarkerFactory instance in use
    110     */
    111    public static IMarkerFactory getIMarkerFactory() {
    112        return MARKER_FACTORY;
    113    }
    114}