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 lombok.EqualsAndHashCode;
33  import lombok.ToString;
34  import org.apache.maven.plugin.logging.Log;
35  import org.slf4j.Marker;
36  import org.slf4j.event.Level;
37  import org.slf4j.helpers.FormattingTuple;
38  import org.slf4j.helpers.LegacyAbstractLogger;
39  import org.slf4j.helpers.MessageFormatter;
40  
41  /**
42   * Implementation of {@link org.slf4j.Logger} transforming SLF4J messages
43   * to Maven log messages.
44   *
45   * <p>The class has too many methods, but
46   * we can't do anything with this since the parent class requires
47   * us to implement them all.
48   *
49   * <p>The class is thread-safe.
50   *
51   * @since 0.1.6
52   * @see <a href="http://www.slf4j.org/faq.html#slf4j_compatible">SLF4J FAQ</a>
53   */
54  @ToString
55  @EqualsAndHashCode(callSuper = false)
56  @SuppressWarnings("PMD.TooManyMethods")
57  final class Slf4jAdapter extends LegacyAbstractLogger {
58  
59      /**
60       * Serialization ID.
61       */
62      public static final long serialVersionUID = 0x12C0976798AB5439L;
63  
64      /**
65       * The log to use.
66       */
67      private final transient Log mlog;
68  
69      /**
70       * The name of the log.
71       */
72      private final transient String label;
73  
74      /**
75       * Public ctor.
76       * @param log The log to use
77       * @param name The label of the logger
78       */
79      Slf4jAdapter(final Log log, final String name) {
80          super();
81          this.mlog = log;
82          this.label = name;
83      }
84  
85      @Override
86      public String getName() {
87          return this.getClass().getName();
88      }
89  
90      @Override
91      public boolean isTraceEnabled() {
92          return false;
93      }
94  
95      @Override
96      public void trace(final String msg) {
97          this.mlog.debug(this.decorate(msg));
98      }
99  
100     @Override
101     public void trace(final String format, final Object arg) {
102         this.mlog.debug(this.decorate(Slf4jAdapter.format(format, arg)));
103     }
104 
105     @Override
106     public void trace(final String format, final Object first,
107         final Object second) {
108         this.mlog.debug(this.decorate(Slf4jAdapter.format(format, first, second)));
109     }
110 
111     @Override
112     public void trace(final String format, final Object... array) {
113         this.mlog.debug(this.decorate(Slf4jAdapter.format(format, array)));
114     }
115 
116     @Override
117     public void trace(final String msg, final Throwable thr) {
118         this.mlog.debug(this.decorate(msg), thr);
119     }
120 
121     @Override
122     public boolean isDebugEnabled() {
123         return this.mlog.isDebugEnabled();
124     }
125 
126     @Override
127     public void debug(final String msg) {
128         this.mlog.debug(this.decorate(msg));
129     }
130 
131     @Override
132     public void debug(final String format, final Object arg) {
133         this.mlog.debug(this.decorate(Slf4jAdapter.format(format, arg)));
134     }
135 
136     @Override
137     public void debug(final String format, final Object first,
138         final Object second) {
139         this.mlog.debug(this.decorate(Slf4jAdapter.format(format, first, second)));
140     }
141 
142     @Override
143     public void debug(final String format, final Object... array) {
144         this.mlog.debug(this.decorate(Slf4jAdapter.format(format, array)));
145     }
146 
147     @Override
148     public void debug(final String msg, final Throwable thr) {
149         this.mlog.debug(this.decorate(msg), thr);
150     }
151 
152     @Override
153     public boolean isInfoEnabled() {
154         return true;
155     }
156 
157     @Override
158     public void info(final String msg) {
159         this.mlog.info(msg);
160     }
161 
162     @Override
163     public void info(final String format, final Object arg) {
164         this.mlog.info(Slf4jAdapter.format(format, arg));
165     }
166 
167     @Override
168     public void info(final String format, final Object first,
169         final Object second) {
170         this.mlog.info(Slf4jAdapter.format(format, first, second));
171     }
172 
173     @Override
174     public void info(final String format, final Object... array) {
175         this.mlog.info(Slf4jAdapter.format(format, array));
176     }
177 
178     @Override
179     public void info(final String msg, final Throwable thr) {
180         this.mlog.info(msg, thr);
181     }
182 
183     @Override
184     public boolean isWarnEnabled() {
185         return true;
186     }
187 
188     @Override
189     public void warn(final String msg) {
190         this.mlog.warn(msg);
191     }
192 
193     @Override
194     public void warn(final String format, final Object arg) {
195         this.mlog.warn(Slf4jAdapter.format(format, arg));
196     }
197 
198     @Override
199     public void warn(final String format, final Object... array) {
200         this.mlog.warn(Slf4jAdapter.format(format, array));
201     }
202 
203     @Override
204     public void warn(final String format, final Object first,
205         final Object second) {
206         this.mlog.warn(Slf4jAdapter.format(format, first, second));
207     }
208 
209     @Override
210     public void warn(final String msg, final Throwable thr) {
211         this.mlog.warn(msg, thr);
212     }
213 
214     @Override
215     public boolean isErrorEnabled() {
216         return true;
217     }
218 
219     @Override
220     public void error(final String msg) {
221         this.mlog.error(msg);
222     }
223 
224     @Override
225     public void error(final String format, final Object arg) {
226         this.mlog.error(Slf4jAdapter.format(format, arg));
227     }
228 
229     @Override
230     public void error(final String format, final Object first,
231         final Object second) {
232         this.mlog.error(Slf4jAdapter.format(format, first, second));
233     }
234 
235     @Override
236     public void error(final String format, final Object... array) {
237         this.mlog.error(Slf4jAdapter.format(format, array));
238     }
239 
240     @Override
241     public void error(final String msg, final Throwable thr) {
242         this.mlog.error(msg, thr);
243     }
244 
245     @Override
246     public String getFullyQualifiedCallerName() {
247         return "jcabi-maven-slf4j";
248     }
249 
250     @Override
251     public void handleNormalizedLoggingCall(final Level level, final Marker marker,
252         final String msg, final Object[] arguments, final Throwable throwable) {
253         throw new UnsupportedOperationException("we should not reach this point ever");
254     }
255 
256     /**
257      * Format with one object.
258      * @param format Format to use
259      * @param arg One argument
260      * @return The message
261      */
262     private static String format(final String format, final Object arg) {
263         final FormattingTuple tuple =
264             MessageFormatter.format(format, arg);
265         return tuple.getMessage();
266     }
267 
268     /**
269      * Format with two objects.
270      * @param format Format to use
271      * @param first First argument
272      * @param second Second argument
273      * @return The message
274      */
275     private static String format(final String format, final Object first,
276         final Object second) {
277         final FormattingTuple tuple =
278             MessageFormatter.format(format, first, second);
279         return tuple.getMessage();
280     }
281 
282     /**
283      * Format with array.
284      * @param format Format to use
285      * @param array List of arguments
286      * @return The message
287      */
288     private static String format(final String format, final Object[] array) {
289         final FormattingTuple tuple =
290             MessageFormatter.format(format, array);
291         return tuple.getMessage();
292     }
293 
294     /**
295      * Decorate a message with a label prefix.
296      * @param msg The text to decorate
297      * @return The message decorated
298      */
299     private String decorate(final String msg) {
300         return String.format(
301             "%s %s: %s",
302             Thread.currentThread().getName(),
303             this.label,
304             msg
305         );
306     }
307 
308 }