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.helpers.FormattingTuple;
36  import org.slf4j.helpers.MarkerIgnoringBase;
37  import org.slf4j.helpers.MessageFormatter;
38  
39  /**
40   * Implementation of {@link org.slf4j.Logger} transforming SLF4J messages
41   * to Maven log messages.
42   *
43   * <p>The class has too many methods, but
44   * we can't do anything with this since the parent class requires
45   * us to implement them all.
46   *
47   * <p>The class is thread-safe.
48   *
49   * @author Yegor Bugayenko (yegor@tpc2.com)
50   * @version $Id$
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 MarkerIgnoringBase {
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      public Slf4jAdapter(final Log log, final String name) {
80          super();
81          this.mlog = log;
82          this.label = name;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public String getName() {
90          return this.getClass().getName();
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public boolean isTraceEnabled() {
98          return false;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void trace(final String msg) {
106         this.mlog.debug(this.decorate(msg));
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public void trace(final String format, final Object arg) {
114         this.mlog.debug(this.decorate(this.format(format, arg)));
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public void trace(final String format, final Object first,
122         final Object second) {
123         this.mlog.debug(this.decorate(this.format(format, first, second)));
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public void trace(final String format, final Object... array) {
131         this.mlog.debug(this.decorate(this.format(format, array)));
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public void trace(final String msg, final Throwable thr) {
139         this.mlog.debug(this.decorate(msg), thr);
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public boolean isDebugEnabled() {
147         return this.mlog.isDebugEnabled();
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public void debug(final String msg) {
155         this.mlog.debug(this.decorate(msg));
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public void debug(final String format, final Object arg) {
163         this.mlog.debug(this.decorate(this.format(format, arg)));
164     }
165 
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public void debug(final String format, final Object first,
171         final Object second) {
172         this.mlog.debug(this.decorate(this.format(format, first, second)));
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     @Override
179     public void debug(final String format, final Object... array) {
180         this.mlog.debug(this.decorate(this.format(format, array)));
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public void debug(final String msg, final Throwable thr) {
188         this.mlog.debug(this.decorate(msg), thr);
189     }
190 
191     /**
192      * {@inheritDoc}
193      */
194     @Override
195     public boolean isInfoEnabled() {
196         return true;
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public void info(final String msg) {
204         this.mlog.info(msg);
205     }
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
211     public void info(final String format, final Object arg) {
212         this.mlog.info(this.format(format, arg));
213     }
214 
215     /**
216      * {@inheritDoc}
217      */
218     @Override
219     public void info(final String format, final Object first,
220         final Object second) {
221         this.mlog.info(this.format(format, first, second));
222     }
223 
224     /**
225      * {@inheritDoc}
226      */
227     @Override
228     public void info(final String format, final Object... array) {
229         this.mlog.info(this.format(format, array));
230     }
231 
232     /**
233      * {@inheritDoc}
234      */
235     @Override
236     public void info(final String msg, final Throwable thr) {
237         this.mlog.info(msg, thr);
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public boolean isWarnEnabled() {
245         return true;
246     }
247 
248     /**
249      * {@inheritDoc}
250      */
251     @Override
252     public void warn(final String msg) {
253         this.mlog.warn(msg);
254     }
255 
256     /**
257      * {@inheritDoc}
258      */
259     @Override
260     public void warn(final String format, final Object arg) {
261         this.mlog.warn(this.format(format, arg));
262     }
263 
264     /**
265      * {@inheritDoc}
266      */
267     @Override
268     public void warn(final String format, final Object... array) {
269         this.mlog.warn(this.format(format, array));
270     }
271 
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public void warn(final String format, final Object first,
277         final Object second) {
278         this.mlog.warn(this.format(format, first, second));
279     }
280 
281     /**
282      * {@inheritDoc}
283      */
284     @Override
285     public void warn(final String msg, final Throwable thr) {
286         this.mlog.warn(msg, thr);
287     }
288 
289     /**
290      * {@inheritDoc}
291      */
292     @Override
293     public boolean isErrorEnabled() {
294         return true;
295     }
296 
297     /**
298      * {@inheritDoc}
299      */
300     @Override
301     public void error(final String msg) {
302         this.mlog.error(msg);
303     }
304 
305     /**
306      * {@inheritDoc}
307      */
308     @Override
309     public void error(final String format, final Object arg) {
310         this.mlog.error(this.format(format, arg));
311     }
312 
313     /**
314      * {@inheritDoc}
315      */
316     @Override
317     public void error(final String format, final Object first,
318         final Object second) {
319         this.mlog.error(this.format(format, first, second));
320     }
321 
322     /**
323      * {@inheritDoc}
324      */
325     @Override
326     public void error(final String format, final Object... array) {
327         this.mlog.error(this.format(format, array));
328     }
329 
330     /**
331      * {@inheritDoc}
332      */
333     @Override
334     public void error(final String msg, final Throwable thr) {
335         this.mlog.error(msg, thr);
336     }
337 
338     /**
339      * Format with one object.
340      * @param format Format to use
341      * @param arg One argument
342      * @return The message
343      */
344     private String format(final String format, final Object arg) {
345         final FormattingTuple tuple =
346             MessageFormatter.format(format, arg);
347         return tuple.getMessage();
348     }
349 
350     /**
351      * Format with two objects.
352      * @param format Format to use
353      * @param first First argument
354      * @param second Second argument
355      * @return The message
356      */
357     private String format(final String format, final Object first,
358         final Object second) {
359         final FormattingTuple tuple =
360             MessageFormatter.format(format, first, second);
361         return tuple.getMessage();
362     }
363 
364     /**
365      * Format with array.
366      * @param format Format to use
367      * @param array List of arguments
368      * @return The message
369      */
370     private String format(final String format, final Object[] array) {
371         final FormattingTuple tuple =
372             MessageFormatter.format(format, array);
373         return tuple.getMessage();
374     }
375 
376     /**
377      * Decorate a message with a label prefix.
378      * @param msg The text to decorate
379      * @return The message decorated
380      */
381     private String decorate(final String msg) {
382         return String.format(
383             "%s %s: %s",
384             Thread.currentThread().getName(),
385             this.label,
386             msg
387         );
388     }
389 
390 }