View Javadoc

1   /**
2    * Copyright (c) 2012-2013, JCabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.slf4j.impl;
31  
32  import lombok.EqualsAndHashCode;
33  import lombok.ToString;
34  import org.apache.maven.plugin.logging.Log;
35  import org.slf4j.ILoggerFactory;
36  import org.slf4j.spi.LoggerFactoryBinder;
37  
38  /**
39   * The binding of {@link ILoggerFactory} class with
40   * an actual instance of {@link ILoggerFactory} is
41   * performed using information returned by this class.
42   *
43   * <p>This is what you should do in your Maven plugin (before everything else):
44   *
45   * <pre> import org.apache.maven.plugin.AbstractMojo;
46   * import org.slf4j.impl.StaticLoggerBinder;
47   * public class MyMojo extends AbstractMojo {
48   *   &#64;Override
49   *   public void execute() {
50   *     StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
51   *     // ... all the rest
52   *   }
53   * }</pre>
54   *
55   * <p>All SLF4J calls will be forwarded to Maven Log.
56   *
57   * <p>The class is thread-safe.
58   *
59   * @author Yegor Bugayenko (yegor@tpc2.com)
60   * @version $Id$
61   * @since 0.1.6
62   * @see <a href="http://www.slf4j.org/faq.html#slf4j_compatible">SLF4J FAQ</a>
63   */
64  @ToString
65  @EqualsAndHashCode(of = "loggers")
66  public final class StaticLoggerBinder implements LoggerFactoryBinder {
67  
68      /**
69       * Declare the version of the SLF4J API this implementation is compiled
70       * against. The value of this field is usually modified with each release.
71       */
72      @SuppressWarnings("PMD.LongVariable")
73      public static final String REQUESTED_API_VERSION = "1.6";
74  
75      /**
76       * The unique instance of this class.
77       */
78      private static final StaticLoggerBinder SINGLETON =
79          new StaticLoggerBinder();
80  
81      /**
82       * The {@link ILoggerFactory} instance returned by the
83       * {@link #getLoggerFactory()} method should always be
84       * the same object.
85       */
86      private final transient JcabiLoggers loggers = new JcabiLoggers();
87  
88      /**
89       * Private ctor to avoid direct instantiation of the class.
90       */
91      private StaticLoggerBinder() {
92          // intentionally empty
93      }
94  
95      /**
96       * Return the singleton of this class.
97       * @return The StaticLoggerBinder singleton
98       */
99      public static StaticLoggerBinder getSingleton() {
100         return StaticLoggerBinder.SINGLETON;
101     }
102 
103     /**
104      * Set Maven Log.
105      * @param log The log from Maven plugin
106      */
107     public void setMavenLog(final Log log) {
108         this.loggers.setMavenLog(log);
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public ILoggerFactory getLoggerFactory() {
116         return this.loggers;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public String getLoggerFactoryClassStr() {
124         return this.loggers.getClass().getName();
125     }
126 
127 }