View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  /**
17   * Copyright (c) 2004-2011 QOS.ch
18   * All rights reserved.
19   *
20   * Permission is hereby granted, free  of charge, to any person obtaining
21   * a  copy  of this  software  and  associated  documentation files  (the
22   * "Software"), to  deal in  the Software without  restriction, including
23   * without limitation  the rights to  use, copy, modify,  merge, publish,
24   * distribute,  sublicense, and/or sell  copies of  the Software,  and to
25   * permit persons to whom the Software  is furnished to do so, subject to
26   * the following conditions:
27   *
28   * The  above  copyright  notice  and  this permission  notice  shall  be
29   * included in all copies or substantial portions of the Software.
30   *
31   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
32   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
33   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
34   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
37   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38   *
39   */
40  package io.netty.util.internal.logging;
41  
42  import org.apache.commons.logging.Log;
43  
44  /**
45   * <a href="http://commons.apache.org/logging/">Apache Commons Logging</a>
46   * logger.
47   */
48  class CommonsLogger extends AbstractInternalLogger {
49  
50      private static final long serialVersionUID = 8647838678388394885L;
51  
52      private final transient Log logger;
53  
54      CommonsLogger(Log logger, String name) {
55          super(name);
56          if (logger == null) {
57              throw new NullPointerException("logger");
58          }
59          this.logger = logger;
60      }
61  
62      /**
63       * Delegates to the {@link Log#isTraceEnabled} method of the underlying
64       * {@link Log} instance.
65       */
66      @Override
67      public boolean isTraceEnabled() {
68          return logger.isTraceEnabled();
69      }
70  
71      /**
72       * Delegates to the {@link Log#trace(Object)} method of the underlying
73       * {@link Log} instance.
74       *
75       * @param msg - the message object to be logged
76       */
77      @Override
78      public void trace(String msg) {
79          logger.trace(msg);
80      }
81  
82      /**
83       * Delegates to the {@link Log#trace(Object)} method of the underlying
84       * {@link Log} instance.
85       *
86       * <p>
87       * However, this form avoids superfluous object creation when the logger is disabled
88       * for level TRACE.
89       * </p>
90       *
91       * @param format
92       *          the format string
93       * @param arg
94       *          the argument
95       */
96      @Override
97      public void trace(String format, Object arg) {
98          if (logger.isTraceEnabled()) {
99              FormattingTuple ft = MessageFormatter.format(format, arg);
100             logger.trace(ft.getMessage(), ft.getThrowable());
101         }
102     }
103 
104     /**
105      * Delegates to the {@link Log#trace(Object)} method of the underlying
106      * {@link Log} instance.
107      *
108      * <p>
109      * However, this form avoids superfluous object creation when the logger is disabled
110      * for level TRACE.
111      * </p>
112      *
113      * @param format
114      *          the format string
115      * @param argA
116      *          the first argument
117      * @param argB
118      *          the second argument
119      */
120     @Override
121     public void trace(String format, Object argA, Object argB) {
122         if (logger.isTraceEnabled()) {
123             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
124             logger.trace(ft.getMessage(), ft.getThrowable());
125         }
126     }
127 
128     /**
129      * Delegates to the {@link Log#trace(Object)} method of the underlying
130      * {@link Log} instance.
131      *
132      * <p>
133      * However, this form avoids superfluous object creation when the logger is disabled
134      * for level TRACE.
135      * </p>
136      *
137      * @param format the format string
138      * @param arguments a list of 3 or more arguments
139      */
140     @Override
141     public void trace(String format, Object... arguments) {
142         if (logger.isTraceEnabled()) {
143             FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
144             logger.trace(ft.getMessage(), ft.getThrowable());
145         }
146     }
147 
148     /**
149      * Delegates to the {@link Log#trace(Object, Throwable)} method of
150      * the underlying {@link Log} instance.
151      *
152      * @param msg
153      *          the message accompanying the exception
154      * @param t
155      *          the exception (throwable) to log
156      */
157     @Override
158     public void trace(String msg, Throwable t) {
159         logger.trace(msg, t);
160     }
161 
162     /**
163      * Delegates to the {@link Log#isDebugEnabled} method of the underlying
164      * {@link Log} instance.
165      */
166     @Override
167     public boolean isDebugEnabled() {
168         return logger.isDebugEnabled();
169     }
170 
171     //
172 
173     /**
174      * Delegates to the {@link Log#debug(Object)} method of the underlying
175      * {@link Log} instance.
176      *
177      * @param msg - the message object to be logged
178      */
179     @Override
180     public void debug(String msg) {
181         logger.debug(msg);
182     }
183 
184     /**
185      * Delegates to the {@link Log#debug(Object)} method of the underlying
186      * {@link Log} instance.
187      *
188      * <p>
189      * However, this form avoids superfluous object creation when the logger is disabled
190      * for level DEBUG.
191      * </p>
192      *
193      * @param format
194      *          the format string
195      * @param arg
196      *          the argument
197      */
198     @Override
199     public void debug(String format, Object arg) {
200         if (logger.isDebugEnabled()) {
201             FormattingTuple ft = MessageFormatter.format(format, arg);
202             logger.debug(ft.getMessage(), ft.getThrowable());
203         }
204     }
205 
206     /**
207      * Delegates to the {@link Log#debug(Object)} method of the underlying
208      * {@link Log} instance.
209      *
210      * <p>
211      * However, this form avoids superfluous object creation when the logger is disabled
212      * for level DEBUG.
213      * </p>
214      *
215      * @param format
216      *          the format string
217      * @param argA
218      *          the first argument
219      * @param argB
220      *          the second argument
221      */
222     @Override
223     public void debug(String format, Object argA, Object argB) {
224         if (logger.isDebugEnabled()) {
225             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
226             logger.debug(ft.getMessage(), ft.getThrowable());
227         }
228     }
229 
230     /**
231      * Delegates to the {@link Log#debug(Object)} method of the underlying
232      * {@link Log} instance.
233      *
234      * <p>
235      * However, this form avoids superfluous object creation when the logger is disabled
236      * for level DEBUG.
237      * </p>
238      *
239      * @param format the format string
240      * @param arguments a list of 3 or more arguments
241      */
242     @Override
243     public void debug(String format, Object... arguments) {
244         if (logger.isDebugEnabled()) {
245             FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
246             logger.debug(ft.getMessage(), ft.getThrowable());
247         }
248     }
249 
250     /**
251      * Delegates to the {@link Log#debug(Object, Throwable)} method of
252      * the underlying {@link Log} instance.
253      *
254      * @param msg
255      *          the message accompanying the exception
256      * @param t
257      *          the exception (throwable) to log
258      */
259     @Override
260     public void debug(String msg, Throwable t) {
261         logger.debug(msg, t);
262     }
263 
264     /**
265      * Delegates to the {@link Log#isInfoEnabled} method of the underlying
266      * {@link Log} instance.
267      */
268     @Override
269     public boolean isInfoEnabled() {
270         return logger.isInfoEnabled();
271     }
272 
273     /**
274      * Delegates to the {@link Log#debug(Object)} method of the underlying
275      * {@link Log} instance.
276      *
277      * @param msg - the message object to be logged
278      */
279     @Override
280     public void info(String msg) {
281         logger.info(msg);
282     }
283 
284     /**
285      * Delegates to the {@link Log#info(Object)} method of the underlying
286      * {@link Log} instance.
287      *
288      * <p>
289      * However, this form avoids superfluous object creation when the logger is disabled
290      * for level INFO.
291      * </p>
292      *
293      * @param format
294      *          the format string
295      * @param arg
296      *          the argument
297      */
298 
299     @Override
300     public void info(String format, Object arg) {
301         if (logger.isInfoEnabled()) {
302             FormattingTuple ft = MessageFormatter.format(format, arg);
303             logger.info(ft.getMessage(), ft.getThrowable());
304         }
305     }
306     /**
307      * Delegates to the {@link Log#info(Object)} method of the underlying
308      * {@link Log} instance.
309      *
310      * <p>
311      * However, this form avoids superfluous object creation when the logger is disabled
312      * for level INFO.
313      * </p>
314      *
315      * @param format
316      *          the format string
317      * @param argA
318      *          the first argument
319      * @param argB
320      *          the second argument
321      */
322     @Override
323     public void info(String format, Object argA, Object argB) {
324         if (logger.isInfoEnabled()) {
325             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
326             logger.info(ft.getMessage(), ft.getThrowable());
327         }
328     }
329 
330     /**
331      * Delegates to the {@link Log#info(Object)} method of the underlying
332      * {@link Log} instance.
333      *
334      * <p>
335      * However, this form avoids superfluous object creation when the logger is disabled
336      * for level INFO.
337      * </p>
338      *
339      * @param format the format string
340      * @param arguments a list of 3 or more arguments
341      */
342     @Override
343     public void info(String format, Object... arguments) {
344         if (logger.isInfoEnabled()) {
345             FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
346             logger.info(ft.getMessage(), ft.getThrowable());
347         }
348     }
349 
350     /**
351      * Delegates to the {@link Log#info(Object, Throwable)} method of
352      * the underlying {@link Log} instance.
353      *
354      * @param msg
355      *          the message accompanying the exception
356      * @param t
357      *          the exception (throwable) to log
358      */
359     @Override
360     public void info(String msg, Throwable t) {
361         logger.info(msg, t);
362     }
363 
364     /**
365      * Delegates to the {@link Log#isWarnEnabled} method of the underlying
366      * {@link Log} instance.
367      */
368     @Override
369     public boolean isWarnEnabled() {
370         return logger.isWarnEnabled();
371     }
372 
373     /**
374      * Delegates to the {@link Log#warn(Object)} method of the underlying
375      * {@link Log} instance.
376      *
377      * @param msg - the message object to be logged
378      */
379     @Override
380     public void warn(String msg) {
381         logger.warn(msg);
382     }
383 
384     /**
385      * Delegates to the {@link Log#warn(Object)} method of the underlying
386      * {@link Log} instance.
387      *
388      * <p>
389      * However, this form avoids superfluous object creation when the logger is disabled
390      * for level WARN.
391      * </p>
392      *
393      * @param format
394      *          the format string
395      * @param arg
396      *          the argument
397      */
398     @Override
399     public void warn(String format, Object arg) {
400         if (logger.isWarnEnabled()) {
401             FormattingTuple ft = MessageFormatter.format(format, arg);
402             logger.warn(ft.getMessage(), ft.getThrowable());
403         }
404     }
405 
406     /**
407      * Delegates to the {@link Log#warn(Object)} method of the underlying
408      * {@link Log} instance.
409      *
410      * <p>
411      * However, this form avoids superfluous object creation when the logger is disabled
412      * for level WARN.
413      * </p>
414      *
415      * @param format
416      *          the format string
417      * @param argA
418      *          the first argument
419      * @param argB
420      *          the second argument
421      */
422     @Override
423     public void warn(String format, Object argA, Object argB) {
424         if (logger.isWarnEnabled()) {
425             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
426             logger.warn(ft.getMessage(), ft.getThrowable());
427         }
428     }
429 
430     /**
431      * Delegates to the {@link Log#warn(Object)} method of the underlying
432      * {@link Log} instance.
433      *
434      * <p>
435      * However, this form avoids superfluous object creation when the logger is disabled
436      * for level WARN.
437      * </p>
438      *
439      * @param format the format string
440      * @param arguments a list of 3 or more arguments
441      */
442     @Override
443     public void warn(String format, Object... arguments) {
444         if (logger.isWarnEnabled()) {
445             FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
446             logger.warn(ft.getMessage(), ft.getThrowable());
447         }
448     }
449 
450     /**
451      * Delegates to the {@link Log#warn(Object, Throwable)} method of
452      * the underlying {@link Log} instance.
453      *
454      * @param msg
455      *          the message accompanying the exception
456      * @param t
457      *          the exception (throwable) to log
458      */
459 
460     @Override
461     public void warn(String msg, Throwable t) {
462         logger.warn(msg, t);
463     }
464 
465     /**
466      * Delegates to the {@link Log#isErrorEnabled} method of the underlying
467      * {@link Log} instance.
468      */
469     @Override
470     public boolean isErrorEnabled() {
471         return logger.isErrorEnabled();
472     }
473 
474     /**
475      * Delegates to the {@link Log#error(Object)} method of the underlying
476      * {@link Log} instance.
477      *
478      * @param msg - the message object to be logged
479      */
480     @Override
481     public void error(String msg) {
482         logger.error(msg);
483     }
484 
485     /**
486      * Delegates to the {@link Log#error(Object)} method of the underlying
487      * {@link Log} instance.
488      *
489      * <p>
490      * However, this form avoids superfluous object creation when the logger is disabled
491      * for level ERROR.
492      * </p>
493      *
494      * @param format
495      *          the format string
496      * @param arg
497      *          the argument
498      */
499     @Override
500     public void error(String format, Object arg) {
501         if (logger.isErrorEnabled()) {
502             FormattingTuple ft = MessageFormatter.format(format, arg);
503             logger.error(ft.getMessage(), ft.getThrowable());
504         }
505     }
506 
507     /**
508      * Delegates to the {@link Log#error(Object)} method of the underlying
509      * {@link Log} instance.
510      *
511      * <p>
512      * However, this form avoids superfluous object creation when the logger is disabled
513      * for level ERROR.
514      * </p>
515      *
516      * @param format
517      *          the format string
518      * @param argA
519      *          the first argument
520      * @param argB
521      *          the second argument
522      */
523     @Override
524     public void error(String format, Object argA, Object argB) {
525         if (logger.isErrorEnabled()) {
526             FormattingTuple ft = MessageFormatter.format(format, argA, argB);
527             logger.error(ft.getMessage(), ft.getThrowable());
528         }
529     }
530 
531     /**
532      * Delegates to the {@link Log#error(Object)} method of the underlying
533      * {@link Log} instance.
534      *
535      * <p>
536      * However, this form avoids superfluous object creation when the logger is disabled
537      * for level ERROR.
538      * </p>
539      *
540      * @param format the format string
541      * @param arguments a list of 3 or more arguments
542      */
543     @Override
544     public void error(String format, Object... arguments) {
545         if (logger.isErrorEnabled()) {
546             FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
547             logger.error(ft.getMessage(), ft.getThrowable());
548         }
549     }
550 
551     /**
552      * Delegates to the {@link Log#error(Object, Throwable)} method of
553      * the underlying {@link Log} instance.
554      *
555      * @param msg
556      *          the message accompanying the exception
557      * @param t
558      *          the exception (throwable) to log
559      */
560     @Override
561     public void error(String msg, Throwable t) {
562         logger.error(msg, t);
563     }
564 }