MarkerFactory xref

View Javadoc

    1   /**
    2    * Copyright (c) 2004-2011 QOS.ch
    3    * All rights reserved.
    4    *
    5    * Permission is hereby granted, free  of charge, to any person obtaining
    6    * a  copy  of this  software  and  associated  documentation files  (the
    7    * "Software"), to  deal in  the Software without  restriction, including
    8    * without limitation  the rights to  use, copy, modify,  merge, publish,
    9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    10   * permit persons to whom the Software  is furnished to do so, subject to
    11   * the following conditions:
    12   *
    13   * The  above  copyright  notice  and  this permission  notice  shall  be
    14   * included in all copies or substantial portions of the Software.
    15   *
    16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23   *
    24   */
    25  package org.slf4j;
    26
    27  import org.slf4j.helpers.BasicMarkerFactory;
    28  import org.slf4j.helpers.Util;
    29  import org.slf4j.impl.StaticMarkerBinder;
    30
    31  /**
    32   * MarkerFactory is a utility class producing {@link Marker} instances as
    33   * appropriate for the logging system currently in use.
    34   *
    35   * <p>
    36   * This class is essentially implemented as a wrapper around an
    37   * {@link IMarkerFactory} instance bound at compile time.
    38   *
    39   * <p>
    40   * Please note that all methods in this class are static.
    41   *
    42   * @author Ceki G&uuml;lc&uuml;
    43   */
    44  public class MarkerFactory {
    45      static IMarkerFactory MARKER_FACTORY;
    46
    47      private MarkerFactory() {
    48      }
    49
    50      /**
    51       * As of SLF4J version 1.7.14, StaticMarkerBinder classes shipping in various bindings
    52       * come with a getSingleton() method. Previously only a public field called SINGLETON
    53       * was available.
    54       *
    55       * @return IMarkerFactory
    56       * @throws NoClassDefFoundError in case no binding is available
    57       * @since 1.7.14
    58       */
    59      private static IMarkerFactory bwCompatibleGetMarkerFactoryFromBinder() throws NoClassDefFoundError {
    60          try {
    61              return StaticMarkerBinder.getSingleton().getMarkerFactory();
    62          } catch (NoSuchMethodError nsme) {
    63              // binding is probably a version of SLF4J older than 1.7.14
    64              return StaticMarkerBinder.SINGLETON.getMarkerFactory();
    65          }
    66      }
    67
    68      // this is where the binding happens
    69      static {
    70          try {
    71              MARKER_FACTORY = bwCompatibleGetMarkerFactoryFromBinder();
    72          } catch (NoClassDefFoundError e) {
    73              MARKER_FACTORY = new BasicMarkerFactory();
    74          } catch (Exception e) {
    75              // we should never get here
    76              Util.report("Unexpected failure while binding MarkerFactory", e);
    77          }
    78      }
    79
    80      /**
    81       * Return a Marker instance as specified by the name parameter using the
    82       * previously bound {@link IMarkerFactory}instance.
    83       *
    84       * @param name
    85       *          The name of the {@link Marker} object to return.
    86       * @return marker
    87       */
    88      public static Marker getMarker(String name) {
    89          return MARKER_FACTORY.getMarker(name);
    90      }
    91
    92      /**
    93       * Create a marker which is detached (even at birth) from the MarkerFactory.
    94       *
    95       * @param name the name of the marker
    96       * @return a dangling marker
    97       * @since 1.5.1
    98       */
    99      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 }