View Javadoc
1   /*
2    * Copyright (c) 2012-2022, 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 com.jcabi.slf4j.JcabiLoggers;
33  import lombok.EqualsAndHashCode;
34  import lombok.ToString;
35  import org.apache.maven.plugin.logging.Log;
36  import org.slf4j.ILoggerFactory;
37  import org.slf4j.spi.LoggerFactoryBinder;
38  
39  /**
40   * The binding of {@link ILoggerFactory} class with
41   * an actual instance of {@link ILoggerFactory} is
42   * performed using information returned by this class.
43   *
44   * <p>This is what you should do in your Maven plugin (before everything else):
45   *
46   * <pre> import org.apache.maven.plugin.AbstractMojo;
47   * import org.slf4j.impl.StaticLoggerBinder;
48   * public class MyMojo extends AbstractMojo {
49   *   &#64;Override
50   *   public void execute() {
51   *     StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
52   *     // ... all the rest
53   *   }
54   * }</pre>
55   *
56   * <p>All SLF4J calls will be forwarded to Maven Log.
57   *
58   * <p>The class is thread-safe.
59   *
60   * @since 0.1.6
61   * @see <a href="http://www.slf4j.org/faq.html#slf4j_compatible">SLF4J FAQ</a>
62   */
63  @ToString
64  @EqualsAndHashCode(of = "loggers")
65  public final class StaticLoggerBinder implements LoggerFactoryBinder {
66  
67      /**
68       * Declare the version of the SLF4J API this implementation is compiled
69       * against. The value of this field is usually modified with each release.
70       */
71      @SuppressWarnings("PMD.LongVariable")
72      public static final String REQUESTED_API_VERSION = "2.0";
73  
74      /**
75       * The unique instance of this class.
76       */
77      private static final StaticLoggerBinder SINGLETON =
78          new StaticLoggerBinder();
79  
80      /**
81       * The {@link ILoggerFactory} instance returned by the
82       * {@link #getLoggerFactory()} method should always be
83       * the same object.
84       *
85       * @checkstyle VisibilityModifierCheck (5 lines)
86       */
87      public final transient JcabiLoggers loggers = new JcabiLoggers();
88  
89      /**
90       * Private ctor to avoid direct instantiation of the class.
91       */
92      private StaticLoggerBinder() {
93          // intentionally empty
94      }
95  
96      /**
97       * Return the singleton of this class.
98       * @return The StaticLoggerBinder singleton
99       */
100     public static StaticLoggerBinder getSingleton() {
101         return StaticLoggerBinder.SINGLETON;
102     }
103 
104     /**
105      * Set Maven Log.
106      * @param log The log from Maven plugin
107      */
108     public void setMavenLog(final Log log) {
109         this.loggers.setMavenLog(log);
110     }
111 
112     /**
113      * Get Maven Log.
114      * @return The log
115      */
116     public Log getMavenLog() {
117         return this.loggers.getMavenLog();
118     }
119 
120     @Override
121     public ILoggerFactory getLoggerFactory() {
122         return this.loggers;
123     }
124 
125     @Override
126     public String getLoggerFactoryClassStr() {
127         return this.loggers.getClass().getName();
128     }
129 
130 }